1 """messagebox消息框"""
2 import tkinter as tk
3 #导入messagebox
4 import tkinter.messagebox as messagebox
5 window = tk.Tk()
6 window.title("My Window")
7 window.geometry("400x400")
8 var = tk.StringVar()
9 var.set("Hello world")
10 label = tk.Label(window,textvariable=var,font=("斜体",15),width=20,height=2,bg="black",fg="green")
11 label.pack()
12 def hit_me():
13 global label #使用全局变量
14 messagebox.showinfo(title="info",message="提示")
15 messagebox.showwarning(title="warning",message="警告")
16 messagebox.showerror(title="error",message="错误")
17 """return yes or no"""
18 question = messagebox.askquestion(title="question",message="问题")
19 if question == "yes":
20 var.set("you selection yes")
21 else:
22 var.set("you selection no")
23 """return True or False"""
24 askyesno = messagebox.askyesno(title="yes or no",message="选择")
25 if askyesno == True:
26 var.set("You selection True")
27 else:
28 var.set("You selection False")
29 """return True or False"""
30 askokcancel = messagebox.askokcancel(title="cancel",message="取消")
31 if askokcancel == True:
32 var.set("You selection cancel")
33 else:
34 var.set("You don't selection cancel")
35 """return True False None"""
36 result = messagebox.askyesnocancel(title="continue",message="Hello World")
37 if result == True:
38 var.set("You selection yes")
39 elif result == False:
40 var.set("You selection no")
41 elif result == None:
42 var.set("You selection cancel")
43 """return True False"""
44 try_cancel = messagebox.askretrycancel(title="try_cancel",message="Hello World")
45 if try_cancel == True:
46 var.set("You selection retry")
47 else:
48 var.set("You selection cancel")
49 button = tk.Button(window,text="hit_me",command=hit_me,fg="red",bg="green")
50 button.pack()
51 window.mainloop()