- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 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_())
最佳答案
这个问题是一个很普遍的错误观念造成的,他们认为QThread是一个Qt Thread,也就是Qt新建的线程,其实不是,QThread
是一个线程处理程序,如the docs 中所示:
The QThread class provides a platform-independent way to manage threads.
A QThread object manages one thread of control within the program. QThreads begin executing in run(). By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread.
在另一个线程上运行的唯一部分是 run 方法,在你的情况下你没有调用是因为你的逻辑不同,你不想连续执行繁重的任务但是应用户的要求,所以设计必须是 worker 的那个,但是您使用 QThread 作为基类,这是不正确的,您必须使用 QObject 作为基类并将其移动到新线程,以便 QObject 在该线程中执行其任务,从而阻止 GUI从阻塞。
一个QObject不是线程安全的,它存在于一个线程中,而它所在的线程由以下几点决定:
QObject
存在于父线程中
如果您没有父线程,除非您已移动到另一个线程,否则将住在创建它的线程中。
并且您可以使用 moveToThread()
函数移动到另一个线程,但是如果 QObject
moveToThread()
将失败有父项,因为第一个条件是特权。
另一方面,如果你想从另一个线程调用一个方法,你必须使用装饰器@QtCore.pyqtSlot()
综合以上,我们得到如下解决方案:
#!/usr/bin/env python3
import sys
import time
from PyQt5 import QtCore, QtWidgets
class ConfWorker(QtCore.QObject):
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)
@QtCore.pyqtSlot()
def doWork(self):
self.request_signal.emit()
@QtCore.pyqtSlot(str)
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(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ConfGUI, self).__init__()
# create a QThread and start the thread that handles
thread = QtCore.QThread(self)
thread.start()
# create the worker without a parent so you can move
self.worker = ConfWorker()
# the worker moves to another thread
self.worker.moveToThread(thread)
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 = QtWidgets.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.doWork)
self.targetBtn.setFixedSize(200, 50)
self.customerlist = QtWidgets.QComboBox(self)
self.customerlist.addItems(["testcustomer1", "testcustomer2", "testcustomer3"])
self.customerlist.setFixedSize(200, 50)
self.label = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignCenter)
self.label.setStyleSheet('font-size: 30pt; font-family: Courier; color: green;')
self.label.setFixedSize(400,50)
self.error_label = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignCenter)
self.error_label.setStyleSheet('font-size: 30pt; font-family: Courier; color: red;')
self.error_label.setFixedSize(400,50)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.customerlist, alignment=QtCore.Qt.AlignCenter)
lay.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
lay.addWidget(self.error_label, alignment=QtCore.Qt.AlignCenter)
lay.addWidget(self.targetBtn, alignment=QtCore.Qt.AlignCenter)
self.setFixedSize(400, 550)
@QtCore.pyqtSlot()
def sendCustomer(self):
self.worker.customer.emit(self.customerlist.currentText())
@QtCore.pyqtSlot(list)
def updateButton(self, button_list):
self.targetBtn.setText(button_list[0])
self.targetBtn.setEnabled(button_list[1])
@QtCore.pyqtSlot(str)
def updateLabel(self, label_text):
self.label.setText(label_text)
@QtCore.pyqtSlot(str)
def updateError(self, error_text):
self.error_label.setText(error_text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = ConfGUI()
ex.show()
sys.exit(app.exec_())
观察:另外,正如您在我的解决方案中看到的那样,QThread
将成为窗口的子级,因为 QThread
是一个 QObject
处理另一个线程。
关于python - 工作线程和主窗口之间的 PyQt5 信号通信工作不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52973090/
我听说过两种数据库架构。 大师级 主从 master-master不是更适合现在的web吗,因为它就像Git一样,每个单元都有整套数据,如果一个宕机也无所谓。 主从让我想起了 SVN(我不喜欢它),你
我们当前将 MySQL 配置为支持故障转移:Site1 Site2。当它们被设置为主/主时。在给定时间点,应用程序服务器仅主动写入一个站点。我们想要设置一个新的故障转移站点。然后我们将拥有 Site
我听说过两种数据库架构。 大师-大师 主从 master-master 不是更适合当今的网络吗,因为它就像 Git,每个单元都有整套数据,如果其中一个发生故障,也没关系。 主从让我想起 SVN(我不喜
我正在创建一个标记为类别的表,其中主类别(父列)包含 0,子类别包含父类别的 ID。我听说这叫引用。我的问题:这张表的结构正确吗?或者是否有更好的方法,例如实现遍历树或类似方法? CREATE TAB
我正在阅读一份关于 C++ 与 C 的文档。该文档说与 C 相比,C++ 编写得非常紧凑。一个例子是,C 允许 main() 函数类型为 void。另一方面,C++ 不允许这样做,他给出了标准中的以下
C main函数和Java main函数有什么区别? int main( int argc, const char* argv[] ) 对比 public static void main(Strin
我一直摸不着头脑,但运气不好。设计器有一个包含 3 栏的站点、两个侧边栏和一个主要内容区域。 专为桌面设计,左栏、主要内容、右栏。但是,在较小的设备上,我们希望首先堆叠主要内容。 所以通常情况下,你可
我一直在阅读有关 Jenkins 主/从配置的信息,但我仍然有一些问题: 是不是真的没有像 Jenkins 主站那样安装和启动从站 Jenkins?我假设我会以相同的方式安装一个主 Jenkins 和
据我了解,Viemodel中MVVM背后的概念包括业务逻辑和/或诸如暴露于 View 的数据的主/明细关系之类的事物 因此,正如我发现的那样,有很多ORM生成器,例如模型的telerik a.o以及另
我们有一个群集,其中包含3个主分区,每个主分区有2个副本。主/副本分片的总文档数相同;但是,对于同一查询/文档,我们得到3个不同的分数。当我们将preference = primary添加为查询参数时
我有一个非常大/旧/长时间运行的项目,它使用相对于启动目录的路径访问文件资源(即应用程序仅在从特定目录启动时才工作)。当我需要调试程序时,我可以从 eclipse 启动它并使用“运行配置”->->“工
谁能向我解释一下为什么我在这段代码上遇到段错误?我一直试图弄清楚这一点,但在各种搜索中却一无所获。当我运行代码而不调用 main(argc, argv) 时,它会运行。 Slave 仅将 argv 中
使用 xcode 中的默认项目作为主从应用程序,如果我在折叠委托(delegate)中放置 print 调试语句,当我旋转设备时它似乎永远不会被触发(事实上我永远无法触发它)。 我编辑的代码位于 Ap
是否有任何产品可以使 mysql 主/从故障转移过程更容易?一些可以自动发生的事情,而不是手动修复它。 最佳答案 [...稍后...;) 你所说的“更容易”是什么?MySQL 有很多解决方案: MyS
我有两个 mysql 数据库。我想做主/主复制。 复制以一种方式进行。然而,反过来说却不然。该错误表明它无法与用户“test@IPADDRESS”连接。 如何将用户名更改为 repl?从未进行过测试,
我正在尝试在 MySQL 中运行以下查询: GRANT REPLICATION SLAVE ON *.* TO 'replication'@’10.141.2.%’ IDENTIFIED BY ‘sl
我正在尝试使用 Android 提供的主/详细流程模板创建一个应用程序,并且我正在尝试将多个操作栏菜单项添加到操作栏的主要部分和详细信息部分。这就是我要实现的目标: (来源:softwarecrew.
我正在寻找一个跨平台的 C++ master/worker 库或工作队列库。一般的想法是我的应用程序将创建某种任务或工作对象,将它们传递给工作主机或工作队列,这将依次在单独的线程或进程中执行工作。为了
我似乎看到很多人在他们的 MySQL 模式中任意分配大尺寸的主/外键字段,例如 INT(11) 甚至 WordPress 使用的 BIGINT(20)。 如果我错了,请纠正我,但即使是 INT(4)
如果我有一个可以与多个键相关联的用户,正确的表设置应该是: 一个表有两列,例如: UserName | Key 没有主键且用户可以有多行,或者: 具有匹配标识符的两个表 Table 1 Us
我是一名优秀的程序员,十分优秀!