您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:PyQt 如何创建自定义QWidget

51自学网 2021-10-30 22:48:48
  python
这篇教程PyQt 如何创建自定义QWidget写得很实用,希望能帮到您。

开发环境

Win7  PyCharm  Python3.5.1  PyQt5

主要文件:

|-- main.py|-- res| `-- fish.jpg`-- ui `-- app_widget.py

main.py

import sysfrom PyQt5.QtWidgets import QApplicationfrom ui.app_widget import AppQWidgetif __name__ == '__main__': app = QApplication(sys.argv) w = AppQWidget() w.show() sys.exit(app.exec_())

app_main_window.py

自定义了一个居中显示的窗口,关闭时弹确认框

from PyQt5.QtCore import QCoreApplicationfrom PyQt5.QtGui import QIconfrom PyQt5.QtWidgets import QWidget, QPushButton, QDesktopWidget, QMessageBoxclass 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_())

以上就是PyQt 如何创建自定义QWidget的详细内容,更多关于PyQt 创建自定义QWidget的资料请关注51zixue.net其它相关文章!


python+selenium小米商城红米K40手机自动抢购的示例代码
解决python 输出到csv 出现多空行的情况
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。