The use of tkinter controls for GUI design in Python (MessageBox controls)

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
insert image description here

showwarning(title,message,options) display warning message
insert image description here

showerror(title,message,options) show error message
insert image description here

askquestion(title,message,options) display asking message
insert image description here

askokcancel(title,message,options) show ok or cancel message
insert image description here
askyesno(title,message,options) ask or no
insert image description here

askyesnocancel(title,message,options) ask yes or no or cancel selection
insert image description here

askretrycancel(title,message,options) show retry or cancel
insert image description here

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
insert image description here

See you next time, bye bye. . .

Guess you like

Origin blog.csdn.net/qq_34699535/article/details/120392132
Recommended