問題描述
我想訪問QThread
類中的父類小部件
I want to access the parent class widgets in the QThread
class
這一行給出了掛起 GUI "Example().setWindowTitle("Window")"
This line gives hangs GUI "Example().setWindowTitle("Window")"
我該怎么做?
class Example(QWidget):
def __init__(self):
super().__init__()
self.myclass2 = myclass2()
self.myclass2.start()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('web.png'))
self.show()
class myclass2(QThread):
def __init__(self, parent=None):
super(myclass2, self).__init__(parent)
def run(self):
while True:
time.sleep(.1)
print(" in thread
")
Example().setWindowTitle("Window")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
推薦答案
你必須明白下面的表達:
You must understand the following expression:
Example().setWindowTitle("Window")
相當于:
obj = Example()
obj.setWindowTitle("Window")
也就是說,您正在創建除 ex = Example()
之外的另一個 Example 對象,并且該對象正在創建另一個 myclass2
對象,以及另一個 myclass2
對象正在創建另一個示例,并且顯然正在創建一個無限循環.
That is, you are creating another Example object other than ex = Example()
, and that object is creating another myclass2
object, and that other myclass2
object is creating another Example, and an infinite loop is clearly being created.
將來可能給您帶來問題的另一件事是為不同的事物建立相同的名稱,雖然在這種情況下這不是問題但在將來的情況下可能會給您帶來問題,我參考的代碼是:
Another thing that in the future could cause you problems is to establish the same name for different things, although in this case it is not a problem but in future occasions it could bring you problems, the code to which I refer is:
self.myclass2 = myclass2()
例如,建議類應以大寫字母開頭.
For example, it is recommended that classes should start with capital letters.
另一個僅在 Qt 中有效的錯誤是不能在主線程以外的線程中創建或操作 GUI.所以你不能直接在另一個線程中更改標題,但有兩種方法:
Another error that is valid only in Qt is that the GUI can not be created or manipulated in a thread other than the main thread. So you can not change the title directly in the other thread but there are 2 methods:
但是為此我們必須通過一個屬性來傳遞 GUI:
But for this we must pass the GUI through a property:
class Example(QWidget):
def __init__(self):
super().__init__()
self.myclass2 = MyClass()
self.myclass2.gui = self
self.myclass2.start()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('web.png'))
self.show()
class MyClass(QThread):
def run(self):
while True:
time.sleep(.1)
print(" in thread
")
QMetaObject.invokeMethod(self.gui, "setWindowTitle", Qt.QueuedConnection, Q_ARG(str, "Window"))
2.信號與插槽
class Example(QWidget):
def __init__(self):
super().__init__()
self.myclass2 = MyClass()
self.myclass2.titleChanged.connect(self.setWindowTitle)
self.myclass2.start()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('web.png'))
self.show()
class MyClass(QThread):
titleChanged = pyqtSignal(str)
def run(self):
while True:
time.sleep(.1)
print(" in thread
")
self.titleChanged.emit("Window")
加:
您不應該直接從線程修改 GUI,而是通過信號發送數據:
You should not modify the GUI from the thread directly but send the data through a signal:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Example(QtWidgets.QWidget):
def __init__(self):
super().__init__()
lay = QtWidgets.QVBoxLayout(self)
le = QtWidgets.QLineEdit()
lay.addWidget(le)
self.myclass2 = MyClass()
self.myclass2.titleChanged.connect(self.setWindowTitle)
self.myclass2.infoChanged.connect(le.setText) # <-- connect signal
self.myclass2.start()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QtGui.QIcon('web.png'))
self.show()
class MyClass(QtCore.QThread):
titleChanged = QtCore.pyqtSignal(str)
infoChanged = QtCore.pyqtSignal(str) # <-- create signal
def run(self):
counter = 0
while True:
QtCore.QThread.msleep(100)
print(" in thread
")
self.titleChanged.emit("Window")
self.infoChanged.emit("{}".format(counter)) # <-- emit signal
counter += 1
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
這篇關于Qthread中的Python訪問父類的小部件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!