【发布时间】:2018-04-16 05:16:24
【问题描述】:
我用 PyQt5 Designer 设计了一个 GUI。
我希望 tryingMethod 始终检查来自服务器的传入消息,所以我创建了一个线程,以便 GUI 和 run 方法可以同时运行,这很有效。
问题是,当我尝试从 Ui_Form 访问变量“Button”时,会引发以下错误:AttributeError: 'Ui_Form' object has no attribute 'Button'。
我尝试将 self 作为tryMethod 线程的参数传递给线程,但它给了我“意外类型:(() -> None, Ui_Form) 警告”
我尝试创建几个不同的类,但我无法解决问题。任何帮助表示赞赏! :)
class Ui_Form(object):
def __init__(self):
t = threading.Thread(target=self.tryingMethod)
t.start()
def tryingMethod(self):
self.Button.setText("TESTING") ##This doesn't work.
while True:
message = self.clientSocket.receive()
def setupUi(self, Form):
//Code has been shortened
self.Button.setFont(font)
self.Button.setCursor(QtGui.QCursor(QtCore.Qt.ForbiddenCursor))
self.Button.setObjectName("Button")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
【问题讨论】:
标签: python multithreading pyqt pyqt5 python-multithreading