效果:点击hit me后弹出对应的messagebox,有的messagebox会有返回值,比如Yes/No,True/False。
然后通过返回值,进行对应的判断操作
import tkinter as tk import tkinter.messagebox # python3需要引入这个模块,不然会报错 # 定义窗口 window = tk.Tk() window.title('my window') # 窗口title window.geometry('200x200') # 窗口尺寸 def hit_me(): # tk.messagebox.showinfo(title='Hi', message='hahahaha') # tk.messagebox.showwarning(title='Hi', message='It is warning') # tk.messagebox.showerror(title='error', message='It is error') # tk.messagebox.askquestion(title='askquestion', message='It is askquestion') # return Yes or No,可通过返回值进行判断操作 # tk.messagebox.askyesno(title='askyesno', message='It is askyesno') # return True or False,可通过返回值进行判断操作 # tk.messagebox.askretrycancel(title='askretrycancel', # message='It is askretrycancel') # return True or False,可通过返回值进行判断操作 tk.messagebox.askokcancel(title='askokcancel', message='It is askokcancel') # return True or False,可通过返回值进行判断操作 tk.Button(window, text='hit me', command=hit_me).pack() window.mainloop()
总结:
1.根据需求选择对应的messagebox
2.python3需要额外引入 tkinter.messagebox 模块,不然会报错