2.10messagebox pop-up window
messagebox widget
In fact, what is here messagebox
is the pop-up window we usually see. button
We first need to define a trigger function to trigger this pop-up window. Here we will put the button we learned before.
tk.Button(window, text='hit me', command=hit_me).pack()
By triggering the function, callmessagebox
def hit_me():
tk.messagebox.showinfo(title='Hi', message='hahahaha')
Click the button button here and a prompt dialog window will pop up.
Here are several forms:
tk.messagebox.showinfo(title='',message='')#提示信息对话窗
tk.messagebox.showwarning()#提出警告对话窗
tk.messagebox.showerror()#提出错误对话窗
tk.messagebox.askquestion()#询问选择对话窗
If the following definition is given, it will print out the value corresponding to our selected item.
def hit_me():
print(tk.messagebox.askquestion(title='Hi', message='hahahaha'))
The renderings
are created in the same way and in the same form.
print(tk.messagebox.askquestion())#返回yes和no
print(tk.messagebox.askokcancel())#返回true和false
print(tk.messagebox.askyesno())#返回true和false
print(tk.messagebox.askretrycancel())#返回true和false
Code
import tkinter as tk
import tkinter.messagebox
window = tk.Tk()
window.title('my window')
window.geometry('200x200')
def hit_me():
#tk.messagebox.showinfo(title='Hi', message='hahahaha') # return 'ok'
#tk.messagebox.showwarning(title='Hi', message='nononono') # return 'ok'
#tk.messagebox.showerror(title='Hi', message='No!! never') # return 'ok'
#print(tk.messagebox.askquestion(title='Hi', message='hahahaha')) # return 'yes' , 'no'
#print(tk.messagebox.askyesno(title='Hi', message='hahahaha')) # return True, False
print(tk.messagebox.asktrycancel(title='Hi', message='hahahaha')) # return True, False
print(tk.messagebox.askokcancel(title='Hi', message='hahahaha')) # return True, False
print(tk.messagebox.askyesnocancel(title="Hi", message="haha")) # return, True, False, None
tk.Button(window, text='hit me', command=hit_me).pack()
window.mainloop()