Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The basic writing of Pyqt5 interface and the basic class encapsulation code

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "the basic writing of the Pyqt5 interface and the basic class encapsulation code". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. The basic writing method of Pyqt5 interface

Target effect

Click the button, the message selection box appears, and the click result of the message selection box is processed.

Effect picture

Second, the basic class encapsulation code

Import sysfrom PyQt5.QtWidgets import QApplication,QWidget,QMessageBox,QPushButtonclass MyClass (QWidget): def _ init__ (self): super (). _ _ init__ () self.initUI () def initUI (self): self.setGeometry (300200400300) self.setWindowTitle ("Liu Jinyu programming") btn=QPushButton ("close form") Self) btn.move (5050) self.show () if _ _ name__== "_ _ main__": app=QApplication (sys.argv) mc=MyClass () app.exec_ ()

Message box

We started using the class library QMessageBox

Take a look at the message categories of common different icons:

Message box with icons. Icons can be question mark question, message information, warning warning

Note: after using the message box, a button type result will be returned, which is also the result of our human-computer interaction.

IV. Rewriting events

Rewrite the thought of closing events: when the close button is clicked, the closing of the form is performed, and the closing of the form is equivalent to clicking the self-contained form button of close X in the upper right corner of the form, which is achieved by the Qwidget base class. Question: how can we achieve the shutdown effect we need?

Override the parent class method.

Overrides the method of closing the event.

QMessageBox uses format

Specific question mark format:

QMessageBox.question (self, message title, message content, button type of message, which button is selected by default after opening it for the first time)

Examples of use:

QMessageBox.question (self, "Liu prompts you:", "do you really want to close the form?" , QMessageBox.Yes | QMessageBox.No,QMessageBox.No)

How do you stop the event from shutting down?

Using the passed event object

We see the properties and methods of the event object in the form of output, and guess the specific calling method.

Event.accept () agrees to close

Event.ignore () flicker operation

If result==QMessageBox.Yes:

Event.accept ()

Else:

Event.ignore ()

QMessageBox.information (self, "message", "Thank you!")

5. Summary and emphasis

1. Master the encapsulation basic code of the class

two。 Master event rewriting of a class

3. The application of mastered event objects

The knowledge source code of this section:

Import sysfrom PyQt5.QtWidgets import QApplication,QWidget,QMessageBox,QPushButton

Class MyClass (QWidget): def _ init__ (self): super (). _ init__ () self.initUI ()

Def initUI (self): self.setGeometry (300200400300) self.setWindowTitle ("Liu Jinyu programming") btn=QPushButton ("close form", self) btn.move (50Power50) btn.clicked.connect (self.close) self.show ()

Def closeEvent (self, event): print ("Lao Liu shuts down operation") result=QMessageBox.question (self, "Lao Liu prompts you:", "do you really want to close the form?" , QMessageBox.Yes | QMessageBox.No,QMessageBox.No) if result==QMessageBox.Yes: event.accept () else: event.ignore () QMessageBox.information (self, "message", "thank you!")

If _ _ name__== "_ _ main__": app=QApplication (sys.argv) mc=MyClass () app.exec_ () "basic Pyqt5 interface writing and basic class encapsulation code" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 296

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report