1. There is a Messagebox module in the tkinter module, which provides 8 dialog boxes for different occasions
showinfo(title,message,options) show general prompt message
showwarning(title,message,options) display warning message
showerror(title,message,options) show error message
askquestion(title,message,options) display asking message
askokcancel(title,message,options) show ok or cancel message
askyesno(title,message,options) ask or no
askyesnocancel(title,message,options) ask yes or no or cancel selection
askretrycancel(title,message,options) show retry or cancel
In the above method, title is the name of the dialog box, message is the text in the dialog box, and options is the selection row parameter (the icon icon can be specified).
2. Up code
from tkinter import*
from tkinter import messagebox
# 弹窗的方法
def myMsg():
messagebox.askyesno("My Messagebox","This is MessageBox controls test")
window=Tk()
window.title("Form")
window.geometry("300x200")
Button(window,text="Pop up messagebox",command=myMsg).pack() # 将弹窗方法绑定至Button的点击事件上
window.mainloop()
3. Above code: Ask whether to end the application
The protocol method of the form is used here.
WM_DELETE_WINDOW in the communication parameter between the form management program and the application program indicates the delete form command of the form management program (Windows manager), which is equivalent to clicking the X in the upper right corner to close the form .
from tkinter import*
from tkinter import messagebox
def callback():
# askokcancel返回一个bool类型,结果取决与用于选择确定或者是取消,选择确定bool结果为true,反之
res=messagebox.askokcancel("询问框","结束或者取消")
if res==True:
root.destroy()
else:
return
root=Tk()
root.title("MessageBox")
root.geometry("300x100")
root.protocol("WM_DELETE_WINDOW",callback)
root.mainloop()
In the above example, askokcancel returns a bool type, the result depends on whether it is used to select OK or cancel, the result of selecting OK bool is true, otherwise...
run
See you next time, bye bye. . .