【发布时间】:2019-03-29 02:39:27
【问题描述】:
我正在使用 PyQt5 创建一个应用程序,它有一个非常简单的用户界面。有一个下拉列表,可以从中选择一个值,然后有一个可以单击的按钮,将根据下拉列表中的当前值执行操作。
目前,如果我运行下面的代码,我可以从下拉列表中获取一个值,它将在myaction.myaction(customer_name) 运行之前发送到工作线程。代码也运行良好,但是当工作线程中的函数运行时,GUI 没有按照我希望的方式工作。当我单击 start 按钮时,它应该向 GUI 发出一个信号以禁用该按钮,更改其标签和颜色,但这从未发生过。函数完成后,应将其改回原来的形式。
问题是我如何处理信号,还是我的课程中有多余的功能?每次单击按钮时将该下拉列表值发送到工作线程的正确方法是什么,所以我可以简单地将它用作那里的变量?
我在网上找到的每一个可能的解决方案都让我很兴奋,但没有一个对我有用,或者其中一些让我太困惑而无法理解。
这是我已经看过的一些答案
#!/usr/bin/env python3
import sys
#import myaction
import time
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QComboBox, QLabel
from PyQt5 import QtCore
class ConfWorker(QtCore.QThread):
updated_button = QtCore.pyqtSignal(list)
updated_label = QtCore.pyqtSignal(str)
updated_error = QtCore.pyqtSignal(str)
request_signal = QtCore.pyqtSignal()
customer = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super(ConfWorker, self).__init__(parent)
self.customer.connect(self.getcustomer)
def run(self):
self.request_signal.emit()
def getcustomer(self, text):
self.configure(text)
def configure(self, customer_name):
self.updated_button.emit(["In progress...", False])
self.updated_label.emit(customer_name)
time.sleep(5) # During this time you should be able to see color change etc.
#myaction.myaction(customer_name)# TAKES ~20 SECONDS TO FINISH
self.updated_button.emit(["Start", True])
class ConfGUI(QWidget):
def __init__(self, parent=None):
super(ConfGUI, self).__init__()
self.worker = ConfWorker(self)
self.worker.updated_button.connect(self.updateButton)
self.worker.updated_label.connect(self.updateLabel)
self.worker.updated_error.connect(self.updateError)
self.worker.request_signal.connect(self.sendCustomer)
self.targetBtn = QPushButton('Start Configuration', self)
self.targetBtn.setStyleSheet("QPushButton { background-color: green; color: white }"
"QPushButton:disabled { background-color: red; color: white }")
self.targetBtn.clicked.connect(self.worker.start)
self.targetBtn.setGeometry(100, 400, 200, 50)
self.setGeometry(800, 300, 400, 550)
self.setFixedSize(400, 550)
self.customerlist = QComboBox(self)
self.customerlist.setGeometry(100, 50, 200, 50)
self.customerlist.setObjectName("customer")
self.customerlist.addItem("testcustomer1")
self.customerlist.addItem("testcustomer2")
self.customerlist.addItem("testcustomer3")
self.label = QLabel(self)
self.label.setText("")
self.label.setStyleSheet('font-size: 30pt; font-family: Courier; color: green;')
self.label.setGeometry(70,250,400,50)
self.error_label = QLabel(self)
self.error_label.setText("")
self.error_label.setStyleSheet('font-size: 30pt; font-family: Courier; color: red;')
self.error_label.setGeometry(70,350,400,50)
self.show()
def sendCustomer(self):
self.worker.customer.emit(self.customerlist.currentText())
def updateButton(self, button_list):
self.targetBtn.setText(button_list[0])
self.targetBtn.setEnabled(button_list[1])
def updateLabel(self, label_text):
self.label.setText(label_text)
def updateError(self, error_text):
self.error_label.setText(error_text)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ConfGUI()
sys.exit(app.exec_())
【问题讨论】:
-
可以分享模块
myaction.py -
@S.Nick 你是什么意思?
-
导入我的动作???
-
@S.Nick 哦,抱歉,这只是我自己的模块,我不允许分享!如果你想测试它,你可以用
time.sleep(n)替换它,并且可能将customer_name 发送到GUI。同样的问题仍然存在。我进行了更改,因此它可以在不导入模块的情况下运行。
标签: python multithreading pyqt pyqt5 qthread