效果:點擊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 模塊,不然會報錯