Tkinter 为了提供了三种标准对话框模块,它们分别是:
messagebox
filedialog
colorchooser
一、 messagebox
- 1、askokcancel
import tkinter as tk
from tkinter import messagebox
def _askokcancel():
tk.messagebox.askokcancel("tkinter demo", "是否提交?")
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))
tk.Button(window,text="提交",command=_askokcancel).pack()
window.mainloop()
image.png
- 、通过返回值来确认用户是点击关闭还是确认
import tkinter as tk
from tkinter import messagebox
def _askokcancel():
re = tk.messagebox.askokcancel("tkinter demo", "是否提交?")
#点击确认re=True 点击取消 re = False
if re:
print("点击确认执行的函数")
else:
print("点击取消执行的函数")
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))
tk.Button(window,text="提交",command=_askokcancel).pack()
window.mainloop()
- 2、askquestion
import tkinter as tk
from tkinter import messagebox
def _askquestion():
re = tk.messagebox.askquestion(title = "tkinter demo", message="是否提交?")
#点击确认re=True 点击取消 re = False
if re:
print("点击确认执行的函数")
else:
print("点击取消执行的函数")
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))
tk.Button(window,text="提交",command=_askquestion).pack()
window.mainloop()
image.png
- 3、askretrycancel
import tkinter as tk
from tkinter import messagebox
def _askretrycancel():
re = tk.messagebox.askretrycancel(title = "tkinter demo", message="是否提交?")
#点击确认re=True 点击取消 re = False
if re:
print("点击确认执行的函数")
else:
print("点击取消执行的函数")
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))
tk.Button(window,text="提交",command=_askretrycancel).pack()
window.mainloop()
image.png
- 4、askyesno
import tkinter as tk
from tkinter import messagebox
def _askyesno():
re = tk.messagebox.askyesno(title="tkinter demo", message="是否提交?")
# 点击确认re=True 点击取消 re = False
if re:
print("点击确认执行的函数")
else:
print("点击取消执行的函数")
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))
tk.Button(window, text="提交", command=_askyesno).pack()
window.mainloop()
image.png
- 5、showerror
import tkinter as tk
from tkinter import messagebox
def _showerror():
re = tk.messagebox.showerror(title="tkinter demo", message="是否提交?")
# 点击确认re=True 点击取消 re = False
if re:
print("点击确认执行的函数")
else:
print("点击取消执行的函数")
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))
tk.Button(window, text="提交", command=_showerror).pack()
window.mainloop()
image.png
- 6、showinfo
import tkinter as tk
from tkinter import messagebox
def _showinfo():
re = tk.messagebox.showinfo(title="tkinter demo", message="是否提交?")
# 点击确认re=True 点击取消 re = False
if re:
print("点击确认执行的函数")
else:
print("点击取消执行的函数")
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))
tk.Button(window, text="提交", command=_showinfo).pack()
window.mainloop()
image.png
- 7、showwarning
import tkinter as tk
from tkinter import messagebox
def _showwarning():
re = tk.messagebox.showwarning(title="tkinter demo", message="是否提交?")
# 点击确认re=True 点击取消 re = False
if re:
print("点击确认执行的函数")
else:
print("点击取消执行的函数")
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))
tk.Button(window, text="提交", command=_showwarning).pack()
window.mainloop()
image.png
参数
所有的这些函数都有相同的参数:
title 参数毋庸置疑是设置标题栏的文本
message 参数是设置对话框的主要文本内容,你可以用 '\n' 来实现换行
options 参数可以设置的选项和含义如下表所示
image.png
返回值
askokcancel(),askretrycancel() 和 askyesno() 返回布尔类型的值:
返回 True 表示用户点击了“确定”或“是”按钮
返回 False 表示用户点击了“取消”或“否”按钮
askquestion() 返回“yes”或“no”字符串表示用户点击了“是”或“否”按钮
showerror(),showinfo() 和 showwarning() 返回“ok”表示用户按下了“是”按钮
二、filedialog
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 callback():
fileName = tk.filedialog.askopenfilename()
print(fileName)#返回的是个文件地址
tk.Button(window, text="打开文件", command=callback).pack()
window.mainloop()
image.png
设置正选择图片:
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 callback():
fileName = tk.filedialog.askopenfilename(filetypes=[("PNG", ".png"), ("JPG", ".jpg"), ("GIF", ".gif")])
print(fileName)
tk.Button(window, text="打开文件", command=callback).pack()
window.mainloop()
参数
两个函数可供设置的选项是一样的,下边列举了可用的选项及含义:
image.png返回值
-
如果用户选择了一个文件,那么返回值是该文件的完整路径
-
如果用户点击了取消按钮,那么返回值是空字符串
三、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 callback():
fileName = tk.colorchooser.askcolor()
print(fileName)
tk.Button(window, text="选择颜色", command=callback).pack()
window.mainloop()
image.png
参数
askcolor(color, **option) 函数的 color 参数用于指定初始化的颜色,默认是浅灰色;
option 参数可以指定的选项及含义如下:
image.png返回值
-
如果用户选择一个颜色并按下“确定”按钮后,返回值是一个 2 元祖,第 1 个元素是选择的 RGB 颜色值,第 2 个元素是对应的 16 进制颜色值
-
如果用户按下“取消”按钮,那么返回值是 (None, None)
网友评论