Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to encapsulate a pyqt5 class in Python

Shulou Source: shulou.com Published: 2022-06-02 07:45:18 11月09日 Update

Today, I will talk to you about how to package a pyqt5 class in Python. Many people may not know much about it. In order to let everyone know more, Xiaobian summarized the following contents for everyone. I hope everyone can gain something according to this article.

I. Code encapsulation ideas

1. Analyze what code needs to be encapsulated: what needs to be encapsulated is the code we configure for the form.

2. Take advantage of inheritance properties of classes

3. Call constructor super() of parent class Qwidget.__ init__()

4. Don't forget to call your own custom method to initialize the UI

II. GUI position size function

GUI position size function using setGeometry

What it does: This function is equivalent to a combination of the resize function and the move function.

Function format:

Control object.setGeometry (X axis, Y axis, width, height)

III. Signal correlation

All signals on the form are monitored by app.exec_() loop.

A signal is just a statement in QT that something happened.

A slot is code executed for a specific event that occurs in QT.

The behavior of GUI on form is the behavior of some event, GUI is driven by event.

Simple and practical signal slot

Format: Signal source. signal.connet

Format explanation: signal source (button). signal (clicked).connet (an event method)

Examples:

#Set button click event to close form

btn.clicked.connect(self.close)

Note: In the process of binding signal slots, our slot method is unbracketed

IV. Summary and emphasis

1. Master the basic idea of class inheritance Qwidget

2. Master the class features cover initUI

3. Master setGeometry function

4. Master the use of simple signal slots

This section of knowledge source code:

import sysfrom PyQt5.QtWidgets import QWidget,QApplication,QPushButton

class MyClass(QWidget): def __init__(self): super().__ init__() self.initUI() def initUI(self): self.setWindowTitle("Liu Jinyu Programming") self.setGeometry(30,40,300,200) # self.resize(300,200) # self.move(30,40) btn=QPushButton("old Liu",self) btn.move(50,60) #Set button click event to close form btn.clicked.connect(self.close) self.show()

if __name__=="__main__": app=QApplication(sys.argv) mc=MyClass() app.exec_()

After reading the above, do you have any further understanding of how to encapsulate a pyqt5 class in Python? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Tags: Signal event function encapsulation form code method content button format location signal source function size knowledge behavior utility function just fit Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Docker Redmi Apple