Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将信号连接到不同线程中的插槽_Python_Multithreading_Pyqt_Pyqt5_Signals Slots - Fatal编程技术网

Python 如何将信号连接到不同线程中的插槽

Python 如何将信号连接到不同线程中的插槽,python,multithreading,pyqt,pyqt5,signals-slots,Python,Multithreading,Pyqt,Pyqt5,Signals Slots,我正在使用Python3.6和PyQt5。我在MainWindow类中绘制了一个gui,其中包括一个QComboBox。我想将currentTextChangedsignal发送到其他线程中的插槽。我对信号和插槽比较陌生。我该怎么做呢?更喜欢类Ui_MainWindowobject的书面示例:向类Threadclass2QtCore.QThread发送信号: 以下是我的代码的缩写版本以及我想要的: from PyQt5 import QtCore, QtGui, QtWidgets from s

我正在使用Python3.6和PyQt5。我在MainWindow类中绘制了一个gui,其中包括一个QComboBox。我想将currentTextChangedsignal发送到其他线程中的插槽。我对信号和插槽比较陌生。我该怎么做呢?更喜欢类Ui_MainWindowobject的书面示例:向类Threadclass2QtCore.QThread发送信号: 以下是我的代码的缩写版本以及我想要的:

from PyQt5 import QtCore, QtGui, QtWidgets
from selenium import webdriver
import time
import threading
from bs4 import BeautifulSoup as soup
import requests

class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            self.keyworddict = {}
            self.count = {}
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(698, 581)
            MainWindow.setMinimumSize(QtCore.QSize(698, 581))
            MainWindow.setMaximumSize(QtCore.QSize(698, 581))
            palette = QtGui.QPalette()
            brush = QtGui.QBrush(QtGui.QColor(154, 161, 161))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
            brush = QtGui.QBrush(QtGui.QColor(206, 206, 206))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
            brush = QtGui.QBrush(QtGui.QColor(214, 214, 214))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
            brush = QtGui.QBrush(QtGui.QColor(154, 161, 161))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
            brush = QtGui.QBrush(QtGui.QColor(206, 206, 206))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
            brush = QtGui.QBrush(QtGui.QColor(214, 214, 214))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
            brush = QtGui.QBrush(QtGui.QColor(154, 161, 161))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
            brush = QtGui.QBrush(QtGui.QColor(214, 214, 214))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
            brush = QtGui.QBrush(QtGui.QColor(214, 214, 214))
            brush.setStyle(QtCore.Qt.SolidPattern)
            palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
            MainWindow.setPalette(palette)
            self.centralWidget = QtWidgets.QWidget(MainWindow)
            self.centralWidget.setObjectName("centralWidget")
            self.comboBox = QtWidgets.QComboBox(self.centralWidget)
            self.comboBox.setGeometry(QtCore.QRect(20, 60, 371, 31))
            font = QtGui.QFont()
            font.setFamily("Yu Gothic")
            font.setPointSize(16)
            self.comboBox.setFont(font)
            self.comboBox.setAcceptDrops(False)
            self.comboBox.setObjectName("comboBox")
            self.comboBox.addItem("")
            self.comboBox.addItem("")
            MainWindow.setCentralWidget(self.centralWidget)
            self.retranslateUi(MainWindow)

        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.comboBox.setItemText(0, _translate("MainWindow", "Jackets"))
            self.comboBox.setItemText(1, _translate("MainWindow", "Shirts"))
class Threadclass2(QtCore.QThread):
    def __init__(self, parent = None):
        super(Threadclass2, self).__init__(parent)

    def run(self):
        print("awd")

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

在此之前,当mainwidnow类仍在绘制gui时,一个按钮将启动线程。添加这个类是关键。干杯

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
    QtWidgets.QMainWindow.__init__(self, parent)
    self.setupUi(self)

    self.thread = Threadclass2(self)
    self.comboBox.currentTextChanged.connect(self.thread.setText)
    self.thread.start()

在某种程度上,这可能取决于代码的多线程部分的结构。因此,您可能应该包括应用程序中的一些示例代码。这个问题与您之前的问题有什么区别?我接受了您的建议,提出了一个更精确的问题。你没有回答我之前的问题和评论。我不知道如何将信号连接到线程槽。正如ekhumuro所说,解决方案取决于线程的实现。@Kermit您不必创建新问题,只需编辑并在以前的问题中指定您的问题,不显示您的代码您已经损失了很多时间,如果您显示它,您可以立即提供帮助:P