代码之家  ›  专栏  ›  技术社区  ›  Aadit

在PyQt4中使用QThread运行线程时更新变量值

  •  0
  • Aadit  · 技术社区  · 7 年前

    所以当我尝试使用 Threading def __init__ 然后用更新的值调用线程及其实例,但不知何故,我无法获得更新的值。

    以下是我的初始代码:

    from PyQt4 import QtGui
    import sys
    import GUI # GUI app by using PYQT4
    from PyQt4.QtCore import QThread
    #import Photos
    
    class PyMain(QtGui.QWidget, GUI.Ui_Pycloud):
        def __init__(self):
            super(self.__class__, self).__init__()
            self.setupUi(self)
            self.password.setEchoMode(QtGui.QLineEdit.Password)
    
            """Picking up data from GUI.py file where initially,
            it is set to 'Username' and 'Password' respectively.
            and initialising `GetThread` class"""
            self.get_thread = GetThread(str(self.username.text()), 
                                        str(self.password.text())
                                       )
            """This is what I was initially using.
               I have tried, only passing the instance and calling 
               get_thread.start() inside __init__ fn but even if i don't click 
               `commandLinkButton` it is somehow called automatically.
               I know it is not the right approach"""
            self.commandLinkButton.clicked.connect(self.get_thread.start)
    
    
    class GetThread(QThread):
    
        def __init__(self, username, password):
            QThread.__init__(self)
            self.username = username
            self.password = password
    
        def __del__(self):
            self.wait()
    
        def authentication(self):
            print self.username, self.password
            # user = Photos.PyPhotos(self.username, self.password)
            # user.authentication(user)
    
        def run(self):
            self.authentication()
    
    
    def main():
        app = QtGui.QApplication(sys.argv)
        form = PyMain()
        form.show()
        app.exec_()
    
    if __name__ == '__main__':
        main()
    

    以下是我尝试的内容:

    ...........................
    ...........................
    ...........................
    
    class PyMain(QtGui.QWidget, GUI.Ui_Pycloud):
      def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.password.setEchoMode(QtGui.QLineEdit.Password)
        self.commandLinkButton.clicked.connect(GetThread(str(self.username.text()), 
                                                 str(self.password.text())).__init__)
    
    class GetThread(QThread):
    
     def __init__(self, username, password):
            QThread.__init__(self)
            self.username = username
            self.password = password
            self.start()
    ...........................
    ...........................
    ...........................
    

    结果: Username Password

    在我运行 main.py commandLinkButton 如果我在我的GUI上更新变量,那么应该更新变量,而这并没有发生。

    编辑: 下面是我再次尝试的内容,它向我显示了正确的输出,如果我在GUI上更新它们,但在这种情况下线程不起作用:

    ..............
    ..............
    ..............
    class PyMain(QtGui.QWidget, GUI.Ui_Pycloud):
        def __init__(self):
            super(self.__class__, self).__init__()
            self.setupUi(self)
            self.password.setEchoMode(QtGui.QLineEdit.Password)
            self.commandLinkButton.clicked.connect(self.populate)
    
        def populate(self):
            get_thread = GetThread(str(self.username.text()), str(self.password.text()))
            get_thread.start()
    
    
    class GetThread(QThread):
    
        def __init__(self, username, password):
            QThread.__init__(self)
            self.username = username
            self.password = password
    
        def __del__(self):
            self.wait()
    
        def authentication(self):
            print self.username, self.password
            user = Photos.PyPhotos(self.username, self.password)
            user.authentication(user)
    
        def run(self):
            self.authentication()
    ......................
    ......................
    ......................
    

    谁能告诉我怎么做?

    1 回复  |  直到 7 年前
        1
  •  2
  •   ekhumoro    7 年前

    您需要使用自定义信号将身份验证结果发送回主线程。但请注意 在主螺纹外。因此,例如,工作线程无法显示身份验证对话框或尝试直接更新小部件。它所能做的就是执行一个漫长的非gui进程,并在完成后将结果发送回主线程。

    代码的基本结构应如下所示:

    class PyMain(QtGui.QWidget, GUI.Ui_Pycloud):
        def __init__(self):
            super(self.__class__, self).__init__()
            self.setupUi(self)
            self.password.setEchoMode(QtGui.QLineEdit.Password)
            self.commandLinkButton.clicked.connect(self.populate)
    
        def populate(self):
            self.thread = GetThread(self.username.text(), self.password.text())
            self.thread.authResult.connect(self.handleAuthResult)
            self.thread.start()
    
        def handleAuthResult(self, result):
            # do something with result...
    
    class GetThread(QThread):
        authResult = QtCore.pyqtSignal(object)
    
        def __init__(self, username, password):
            QThread.__init__(self)
            self.username = username
            self.password = password
    
        def authentication(self):
            result = do_authentication()
            self.authResult.emit(result)
    
        def run(self):
            self.authentication()