Tkinter提供了三种标准对话框模块,分别是:messagebox、filedialog、colorchooser
messagebox(消息对话框)
实例1:askokcancel函数
1 from tkinter import *
2
3 print(messagebox.askokcancel("FishC Demo","发射核弹?"))
4
5 mainloop()
实例2:askquestion函数
实例3:asiretrycancel函数
实例4:askyesno函数
实例5:showerror函数
1 from tkinter import *
2
3 #print(messagebox.askokcancel("FishC Demo","发射核弹?"))
4 #print(messagebox.askquestion("FishC Demo","买个U盘?"))
5 #print(messagebox.askretrycancel("FishC Demo","启动失败,重启?"))
6 #print(messagebox.askyesno("FishC Demo","你确定要格式化硬盘吗?"))
7 print(messagebox.showerror("FishC Demo","Error!!!"))
8
9 mainloop()
实例6:showinfo函数
1 from tkinter import *
2
3 #options参数可设置为default、icon与parent
4 #print(messagebox.askokcancel("FishC Demo","发射核弹?"))
5 #print(messagebox.askquestion("FishC Demo","买个U盘?"))
6 #print(messagebox.askretrycancel("FishC Demo","启动失败,重启?"))
7 #print(messagebox.askyesno("FishC Demo","你确定要格式化硬盘吗?"))
8 #print(messagebox.showerror("FishC Demo","Error!!!"))
9 messagebox.showinfo("Hadley","Great!!!",icon="info")
10
11 mainloop()
实例7:showwarning函数
1 from tkinter import *
2
3 #options参数可设置为default、icon与parent
4 #print(messagebox.askokcancel("FishC Demo","发射核弹?"))
5 #print(messagebox.askquestion("FishC Demo","买个U盘?"))
6 #print(messagebox.askretrycancel("FishC Demo","启动失败,重启?"))
7 #print(messagebox.askyesno("FishC Demo","你确定要格式化硬盘吗?"))
8 #print(messagebox.showerror("FishC Demo","Error!!!"))
9 #messagebox.showinfo("Hadley","Great!!!",icon="info")
10 messagebox.showwarning("Hadley","Warning!!!",icon="warning")
11
12 mainloop()
filedialog(文本对话框)
当应用程序需要使用打开文件或保存文件的功能时
实例1:
1 from tkinter import *
2
3 root = Tk()
4
5 def callback():
6 #askopenfilename函数用来打开文件
7 #asksaveasfilename函数用来保存文件
8 fileName = filedialog.askopenfilename()
9 print(fileName)
10
11 Button(root,text="打开文件夹",command=callback).pack()
12
13 mainloop()
实例2:限制打开文件类型
1 from tkinter import *
2
3 root = Tk()
4
5 def callback():
6 #askopenfilename函数用来打开文件
7 #asksaveasfilename函数用来保存文件
8 #fileName = filedialog.askopenfilename()
9 #限制打开文件类型
10 fileName = filedialog.askopenfilename(filetypes=[("PNG",".png"),("GIF",".gif")])
11 print(fileName)
12
13 Button(root,text="打开文件夹",command=callback).pack()
14
15 mainloop()
colorchooser(颜色选择对话框)
颜色对话框提供一个让用户选择颜色的界面
实例1:
1 from tkinter import *
2
3 root = Tk()
4
5 def callback():
6 #colorchooser函数用于打开颜色选择对话框
7 fileName = colorchooser.askcolor()
8 print(fileName)
9
10 Button(root,text="打开文件夹",command=callback).pack()
11
12 mainloop()
对应的RGB值及其对应的16进制值