This article mainly for you in detail the PYQT5 every day must learn pop-up message box, with a certain reference value, interested in small partners can refer to
By default, if we click the X button on the title bar, Qwidget closes. Sometimes, we need to change this default behavior. For example, if we have a file that we want to open in the editor, we can first display a message box confirming the operation of the open or not.
#!/usr/bin/python3#-*-coding:utf-8-*-"" "PyQt5 Tutorial when we click the Close button of the application window, the program displays a confirmation message box. Author: My World You've been to the blog: Http://blog.csdn.net/weiaitaowang Last edited: July 29, 2016 "" "Import sysfrom pyqt5.qtwidgets Import Qapplication, Qwidget, Qmessageboxclass Example (qwidget): def __init__ (self): super (). __init__ ( ) Self.initui () def initui (self): self.setgeometry ( "the Message box") Self.setwindowtitle Self.show () def closeevent (Self, event): reply = Qmessagebox.question (self, ' info ', ' Confirm exit? ', Qmessagebox.yes | Qmessagebox.no, qmessagebox.no) if reply = = Qmessagebox.yes: event.accept () Else: Event.ignore () if __name__ = = ' __main__ ': app = Qapplication (sys.argv) ex = Example () sys.exit (App.exec_ ())
If we close the Qwidget control, the Qcloseevent event will be generated. Modifying control properties We need to re-implement the Closeevent () event handler.
Reply = Qmessagebox.question (self, ' info ', ' Confirm exit? ', Qmessagebox.yes | Qmessagebox.no, qmessagebox.no)
We display a message box with two buttons: Yes and No. The first string appears in the title bar. The second string is the message text that is displayed in the dialog box. The third parameter specifies a combination of buttons in the popup dialog box. The last parameter is the default button, which is the button with the initial keyboard focus. The return value is stored in the reply variable.
if reply = = Qmessagebox.yes: event.accept () Else: Event.ignore ()
Here we use the if test to return a value. If we click the Yes button, we accept the close of the button control and execute the application's termination event. Otherwise, we ignore the shutdown event.
Click on the x popup in the upper right corner to exit the message box after the program executes