簡體   English   中英

在PyQt4中使用QThread運行線程時更新變量值

[英]Updating variable values when running a thread using QThread in PyQt4

因此,當我嘗試在代碼中使用Threading時發生了問題。 我想做的是將默認值傳遞給def __init__ ,然后使用具有更新值的實例調用線程,但是以某種方式我無法獲取更新值。

下面是我的初始代碼: main.py

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文件的那一刻就會顯示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()
......................
......................
......................

所以任何人都可以告訴我該如何處理?

您需要使用自定義信號將身份驗證結果發送回主線程。 但是請注意,您不得在主線程之外執行任何類型的gui操作 因此,例如,工作線程無法顯示身份驗證對話框或嘗試直接更新窗口小部件。 它所能做的就是執行一個冗長的non-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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM