PyQt 如何创建自定义QWidget
作者:AnRFDev 时间:2023-09-13 17:30:48
目录
开发环境
主要文件:
main.py
app_main_window.py
Tips
多控件可以存在list中
QApplication与QWidget
注意widget持有外部对象引用的问题
开发环境
Win7 PyCharm Python3.5.1 PyQt5
主要文件:
|-- main.py
|-- res
| `-- fish.jpg
`-- ui
`-- app_widget.py
main.py
import sys
from PyQt5.QtWidgets import QApplication
from ui.app_widget import AppQWidget
if __name__ == '__main__':
app = QApplication(sys.argv)
w = AppQWidget()
w.show()
sys.exit(app.exec_())
app_main_window.py
自定义了一个居中显示的窗口,关闭时弹确认框
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget, QPushButton, QDesktopWidget, QMessageBox
class AppQWidget(QWidget):
"""
A custom QWidget by Rust Fisher
"""
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
# self.setGeometry(300, 300, 400, 200) # 相当于move和resize
self.resize(300, 200)
self.move_to_center()
self.setWindowTitle('Demo1')
self.setWindowIcon(QIcon('res/fish.jpg'))
btn1 = QPushButton('Quit', self)
btn1.setToolTip('Click to quit')
btn1.resize(btn1.sizeHint())
btn1.move(200, 150)
btn1.clicked.connect(QCoreApplication.instance().quit) # cannot locate function connect
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Message',
'Are you sure to quit now?',
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def move_to_center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center() # got center info here
qr.moveCenter(cp)
self.move(qr.topLeft()) # 应用窗口的左上方的点到qr矩形的左上方的点,因此居中显示在我们的屏幕上
Tips
多控件可以存在list中
存在一起,需要对整体操作时直接遍历列表
# 同组的控件可以存在同一个list中
self.cb_list = [
self.ma.i2cCB,
self.ma.mipiCB,
self.ma.eepromCB,
self.ma.tem_sensorCB,
self.ma.lensCB,
self.ma.vcmCB,
self.ma.mirrorCB,
self.ma.mirrorCaliCB, ]
self.test_count_et_list = [
self.ma.i2cCountEt,
self.ma.mipiCountEt,
self.ma.eepromCountEt,
self.ma.tem_sensorCountEt,
self.ma.lensCountEt,
self.ma.vcmCountEt,
self.ma.mirrorCountEt,
self.ma.mirrorCaliCountEt,
]
# 需要操作某组控件时 直接遍历列表
def _click_test_item_cb(self):
""" Update [choose all checkbox] by all test item state """
choose_all = True
for cb in self.cb_list:
choose_all = choose_all & cb.isChecked()
self.ma.selecteAllCB.setChecked(choose_all)
QApplication与QWidget
QApplication是一个单例,在QWidget中可以通过QApplication.instance()获取到对象
实际上在实例化QApplication前就使用QtGui.QWidget()是会报错的
>>> QtGui.QWidget()
QWidget: Must construct a QApplication before a QPaintDevice
参考 How QApplication() and QWidget() objects are connected in PySide/PyQt?
在我们自定义的QMainWindow中,也可以直接获取到QApplication的实例。
class RustMainWindow(QMainWindow):
""" This is the main class """
def _trigger_english(self):
print "Change to English", QApplication.instance()
# Change to English <PyQt4.QtGui.QApplication object at 0x02ABE3A0>
注意widget持有外部对象引用的问题
如果在程序启动的地方将引用交给widget,退出时会造成应用无法关闭的问题(类似内存泄漏)。
if __name__ == '__main__':
app = QApplication(sys.argv)
# 这里把app交给了MainWindow,MainWindow关闭时是无法正常退出应用的
main_d = RustMainWindow(app) # 不建议这么做
main_d.show()
sys.exit(app.exec_())
来源:https://cloud.tencent.com/developer/article/1783146
标签:python,PyQt,自定义,QWidget
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mysql常用函数汇总(分享)
2024-01-29 03:30:56
python调用matlab的方法详解
2023-10-18 06:39:02
![](https://img.aspxhome.com/file/2023/6/118566_0s.png)
聊聊python里如何用Borg pattern实现的单例模式
2021-06-26 23:28:38
javascript中Array()数组函数详解
2024-04-17 10:23:36
对pytorch中不定长序列补齐的操作
2022-03-24 17:33:04
Python+Pygame实战之疯狂吃水果游戏的实现
2021-09-17 11:37:58
![](https://img.aspxhome.com/file/2023/0/88040_0s.gif)
python实操练习案例(六)
2021-01-06 08:09:23
![](https://img.aspxhome.com/file/2023/3/127843_0s.png)
Python中异常捕获与处理的方法总结
2023-12-10 07:48:24
![](https://img.aspxhome.com/file/2023/8/95898_0s.png)
mysql5.7.20第一次登录失败的快速解决方法
2024-01-19 16:57:36
![](https://img.aspxhome.com/file/2023/2/122912_0s.png)
CSS如何做细线表格
2009-01-09 13:12:00
JS在IE和FireFox之间常用函数的区别小结
2024-04-16 09:54:54
重构Python代码的六个实例
2023-08-07 02:10:14
Mysql命令大全(完整版)
2024-01-13 15:03:35
pandas 空的dataframe 插入列名的示例
2023-10-27 07:48:18
Asp.net清空控件值的方法(可自定义控件类型)
2023-07-22 23:23:16
Python访问MongoDB,并且转换成Dataframe的方法
2022-07-18 20:32:07
php发送get、post请求的6种方法简明总结
2023-11-14 12:59:43
浅析Python3爬虫登录模拟
2023-10-10 18:15:02
vue中记录滚动条位置的两种方法
2024-04-27 15:59:33
python构造函数init实例方法解析
2022-09-30 15:09:19
![](https://img.aspxhome.com/file/2023/2/103612_0s.png)