PYQT5 every day must learn pop-up message box

Source: Internet
Author: User
Tags first string
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.