14_对话框模块

对话框模块

messagebox
filedialog
colorchooser

1,messagebox - 提示信息

import tkinter as tk
from tkinter import messagebox

window = tk.Tk()
window.title('hello thinter')
height = window.winfo_screenheight()
width = window.winfo_screenwidth()
window.geometry('400x300+%d+%d' % ((width - 400) / 2, (height - 300) / 2))

"""
askokcancel
askquestion
askretrycancel
askyesno
showerror
showinfo
showwarning
"""

def _askokcancel():
    re = tk.messagebox.askokcancel("askokcancel", "是否提交?")
    if re:
        print(re, "点击确认执行的函数")
    else:
        print(re, "点击取消执行的函数")

def _askquestion():
    re = tk.messagebox.askquestion(title="askquestion", message="是否提交?")
    if re:
        print(re, "点击确认执行的函数")
    else:
        print(re, "点击取消执行的函数")

def _askretrycancel():
    re = tk.messagebox.askretrycancel(title="askretrycancel", message="是否提交?")
    if re:
        print(re, "点击确认执行的函数")
    else:
        print(re, "点击取消执行的函数")

def _askyesno():
    re = tk.messagebox.askyesno(title="askyesno", message="是否提交?")
    if re:
        print(re, "点击确认执行的函数")
    else:
        print(re, "点击取消执行的函数")

def _showerror():
    re = tk.messagebox.showerror(title="showerror", message="error提示?")
    print(re, "点击确认执行的函数")

def _showinfo():
    re = tk.messagebox.showinfo(title="showinfo", message="info提示?")
    print(re, "点击确认执行的函数")

def _showwarning():
    re = tk.messagebox.showwarning(title="showwarning", message="warning提示?")
    print(re, "点击确认执行的函数")
    
tk.Button(window, text="提交", command=_askokcancel).pack()
tk.Button(window, text="提交", command=_askquestion).pack()
tk.Button(window, text="提交", command=_askretrycancel).pack()
tk.Button(window, text="提交", command=_askyesno).pack()
tk.Button(window, text="提交", command=_showerror).pack()
tk.Button(window, text="提交", command=_showinfo).pack()\
tk.Button(window, text="提交", command=_showwarning).pack()

window.mainloop()

title 参数毋庸置疑是设置标题栏的文本
message 参数是设置对话框的主要文本内容,你可以用 '\n' 来实现换行
options 参数可以设置的选项和含义如下表所示

选项 含义
default 1,设置默认的按钮(按下回车响应的按钮)
2,默认是第一个按钮(像“确定”,“是”或“重试”)
3,可选:CANCEL、IGNORE、OK、NO、RETRY、YES
icon 1,指定对话框显示的图标
2,可选值:ERROR、INFO、QUESTION、WARNING
3,注意:不能指定自定义图标
parent 1,如果不指定该选项,那么对话框默认显示在根窗口上
2,如果想要将对话框显示在子窗口w上,那么可以设置parent=w

2,filedialog - 文件选择

import tkinter as tk
from tkinter import filedialog

window = tk.Tk()
window.title('hello thinter')
height = window.winfo_screenheight()
width = window.winfo_screenwidth()
window.geometry('400x300+%d+%d' % ((width - 400) / 2, (height - 300) / 2))

def choosefile():
    filepath = tk.filedialog.askopenfilename(title='选择单个附件',filetypes=[("PNG", ".png"), ("TXT", ".txt"), ('All', '*.*')])
    # filespath = tk.filedialog.askopenfilenames(title='选择多个附件')
    print(filepath)	# 不选择文件,返回空字符串

def openfile():
    openfile = tk.filedialog.askopenfile()  # 打开单个文件
    openfiles = tk.filedialog.askopenfiles()  # 打开多个文件
    openfile.read()

tk.Button(window, text="选择文件", command=choosefile).pack()
window.mainloop()
选项 含义
defaultextension 1,指定文件的后缀
2,例:defaultextension=".jpg",输入文件名"logo",会自动添加后缀为"logo.jpg"
3,注意:如果输入文件名包含后缀,那么该选项不生效
filetypes 1,指定筛选文件类型的下拉菜单选项
2,该选项元祖构成的列表,例 filetypes=[("PNG", ".png"), ("TXT", ".txt"), ('All', '.')]
initialdir 指定打开/保存文件的路径,默认为当前文件夹
parent 1,如果不指定该选项,那么对话框默认显示在根窗口上
2,如果想要将对话框显示在子窗口w上,那么可以设置parent=w
title 文件选择对话框的标题栏文本

3,colorchooser - 颜色选择

import tkinter as tk
from tkinter import colorchooser

window = tk.Tk()
window.title('hello thinter')
height = window.winfo_screenheight()
width = window.winfo_screenwidth()
window.geometry('400x300+%d+%d' % ((width - 400) / 2, (height - 300) / 2))

def choosecolor():
    color_number = tk.colorchooser.askcolor(color='red')	# 颜色选择器
    print(color_number)	# 返回((R,G,B),'16进制颜色') 或 (None,None)

tk.Button(window, text="选择颜色", command=choosecolor).pack()
window.mainloop()
选项 含义
color 指定初始选择的颜色,默认浅灰色
parent 1,如果不指定该选项,那么对话框默认显示在根窗口上
2,如果想要将对话框显示在子窗口w上,那么可以设置parent=w
title 文件选择对话框的标题栏文本
posted @   interesting5319  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示