【发布时间】:2021-12-01 10:47:48
【问题描述】:
关于 PyQt5 包的pyqtProperty 类和python 的标准内置property 类,我有几个问题:
-
为什么在标准属性上使用
pyqtProperty?下面给出的代码以两种方式工作。使用pyqtProperty而不是 Python 的普通旧property(用作方法的装饰器)有什么优势?我知道我必须指定pyqtProperty的返回类型,但我为什么要使用它呢? -
pyqtProperty是否继承自property?属性pyqtProperty.__mro__没有显示(结果是PyQt5.QtCore.pyqtProperty, object),我找不到pyqtProperty的源代码文件。 PyDoc's 命令引用模块PyQt5.QtCore的.pyd文件(不是源代码纯文本文件),并且没有存根文件。但是命令help(pyqtProperty)给我的信息非常相似 与help(property)获得的信息,不同 在official documentaion 上找到的信息。那么,这些信息从何而来? -
为什么用
@pyqtProperty甚至@property装饰的方法在设置Qobject 时执行?回到下面给出的代码:属性方法(在这两种情况下为
@pyqtProperty和@property)在创建属性_custom_property之前执行,它给出了AttributeError。但这不应该发生,并且对于带有常规 python 类的@property装饰器确实不会发生(我也尝试过,下面有一个片段)。当我使用简单的 getter 和 setter 方法(未修饰)时,根本不会发生这种情况(这是正常的)。最奇怪的现象是:这个AttributeError是我在调试模式下运行的时候出现的。在正常模式 (python main.py) 下,属性方法无论如何都会执行,但不会引发错误。
我正在使用 PyQt5 版本 5.9.2 与 Anaconda Distribution,python 3.8 一起安装。
这是使用pyuic5生成的Ui_MainWindow.py文件的内容:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(419, 196)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 419, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lineEdit.setText(_translate("MainWindow", "test"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
这是使用property装饰器的main.py文件的内容:
from Ui_MainWindow import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self._custom_property = 1
@property
def custom_property(self):
print("This method is executed")
return self._custom_property
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
这是使用pyqtProperty装饰器的main.py文件的内容:
from Ui_MainWindow import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import pyqtProperty
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self._custom_property = 1
@pyqtProperty(int)
def custom_property(self):
print("This method is executed")
return self._custom_property
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
我不会放没有装饰器的主文件的内容(只需删除上面代码中的@ 行)。
但这是一个带有property 装饰器的简单文件,表明它没有在__init__ 方法中执行
class Test:
def __init__(self) -> None:
pass
@property
def x(self):
print("This method is not executed in init")
return self._x
def setx(self,value):
self._x = value
if __name__ == "__main__":
a = Test()
【问题讨论】:
标签: python python-3.x properties pyqt5