当前位置:网站首页>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_())
边栏推荐
- IP network active evaluation system -- x-vision
- 三元表达式、各生成式、匿名函数
- php 获取图片信息的方法
- Use partial derivatives to display normals in unity
- operator
- Overseas agent recommendation
- 【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
- Jerry's about TWS channel configuration [chapter]
- Ueeditor custom display insert code
- [azure microservice service fabric] how to transfer seed nodes in the service fabric cluster
猜你喜欢
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
【Azure微服务 Service Fabric 】如何转移Service Fabric集群中的种子节点(Seed Node)
Pdf document signature Guide
How to choose the appropriate automated testing tools?
What if the win11u disk does not display? Solution to failure of win11 plug-in USB flash disk
The strongest installation of the twin tower model, Google is playing "antique" again?
The whole network "chases" Zhong Xuegao
反爬通杀神器
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
随机推荐
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
强化学习-学习笔记9 | Multi-Step-TD-Target
UWA问答精选
Leetcode SQL first day
Add get disabled for RC form
Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
The function is really powerful!
How to write an augmented matrix into TXT file
How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525
C development - interprocess communication - named pipeline
How to choose the appropriate automated testing tools?
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
Vs custom template - take the custom class template as an example
【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式
Two methods of calling WCF service by C #
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
ByteDance Android interview, summary of knowledge points + analysis of interview questions
Programming mode - table driven programming
UWA Q & a collection
C # Development -- pit encountered in JS intermodulation