当前位置:网站首页>Pyqt GUI interface and logic separation
Pyqt GUI interface and logic separation
2022-07-07 22:27:00 【Eva215665】
analysis Visual design form Qt Designer Design GUI Single form program This example , You can find , It achieved the right Ui_FormHello Use , Generated GUI Program , But it's flawed . because appMain1.py It's completely a procedural procedure , It is difficult to realize the effective encapsulation of business logic and functions . This paper introduces the design method of separating single inheritance interface from business logic .
Still in Visual design form Qt Designer Design GUI Single form program Used in the example project Next , Write another file appMain.py, In this file
(1) Define the form business logic class QmyWidget, Single inheritance QWidget class
(2) stay QmyWidget In the constructor of , First, call the constructor of the parent class , such self It's already a QWidget Object
(3) Show create a Ui_FormHello Private properties of class self.__ui, This private property contains all components on the form , Only through this object can the component be accessed , Including calling setupUi function
(4)self.__ui It's a private property , Cannot access outside of class , To access components , You need to define interface functions to realize a series of functions . For example, designing public functions setBtnText Used to set the text on the button
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from FormHello import Ui_FormHello
class QmyWidget (QWidget):
def __init__(self, parent = None):
super().__init__(parent) # adopt super Call parent constructor , establish QWidget forms , such self It's just a form object
self.__ui = Ui_FormHello() # establish UI object , Private property __ui Including visual design UI All components on the form , So only through
# self.__ui To access the components on the form , Including calling setupUi function
# and __ui It's a private property , Create objects outside the class , You cannot access components on the form through objects , To access components , You can define interfaces , Realization function
self.__ui.setupUi(self) # establish UI
self.__ui.LabHello.setText(" Single inherited QmyWidget")
# Definition setBtnText The interface function , Used to set the text on the button
def setBtnText(self, aText):
self.__ui.btnClose.setText(aText)
# establish app
if __name__ == "__main__":
app = QApplication(sys.argv) # establish app, use QApplication class
myWidget = QmyWidget()
myWidget.setBtnText(" Indirect settings ")
myWidget.show()
sys.exit(app.exec_())
边栏推荐
- Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
- Remember aximp once Use of exe tool
- C development -- WPF simple animation
- operator
- What is the difference between the three values of null Nan undefined in JS
- Relationship between URL and URI
- What does it mean to prefix a string with F?
- How pyGame rotates pictures
- SAR image quality evaluation
- The function is really powerful!
猜你喜欢

谈谈制造企业如何制定敏捷的数字化转型策略

Firefox browser installation impression notes clipping

PKPM 2020 software installation package download and installation tutorial

Antd date component appears in English
![Jerry's manual matching method [chapter]](/img/92/74281c29565581ecb761230fbfd0f3.png)
Jerry's manual matching method [chapter]

Crawler (17) - Interview (2) | crawler interview question bank

Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission

Paint basic graphics with custompaint

OpenGL configuration vs2019
![Jerry's about TWS channel configuration [chapter]](/img/94/fde5054fc412b786cd9864215e912c.png)
Jerry's about TWS channel configuration [chapter]
随机推荐
#DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
Antd date component appears in English
Matplotlib快速入门
客户案例|华律网,通过观测云大幅缩短故障定位时间
Revit secondary development - project file to family file
The strongest installation of the twin tower model, Google is playing "antique" again?
Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
Ren Qian code compilation error modification
Latest Android advanced interview questions summary, Android interview questions and answers
Record layoutrebuild Forcerebuildlayoutimmediate does not take effect
Matplotlib drawing interface settings
戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
PKPM 2020软件安装包下载及安装教程
How pyGame rotates pictures
Embedded development: how to choose the right RTOS for the project?
Which futures company is the safest to open a futures account?
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
[interview arrangement] 0211 game engine server
Main functions of OS, Sys and random Standard Libraries
Failed to initialize rosdep after installing ROS