将参数传递给PyQt5

时间:2015-12-20 11:45:44

标签: python-3.x qthread pyqt5

我是PyQt5中的新手,当我尝试将参数传递给QThread子类时出现了问题。这是代码:

class DrClientThread(QtCore.QThread):
    def __int__(self, server, username, password):
        QtCore.QThread.__init__(self)
        self.server = server
        self.username = username
        self.password = password

    def run(self):
        print(self.username, self.password, self.server)

class DrCOM_UI(Ui_Login_Dialog):
    def __init__(self, dialog):
        Ui_Login_Dialog.__init__(self)
        self.setupUi(dialog)
        self.login_btn.clicked.connect(self.login_and_keep_alive)
        ...
    def login_and_keep_alive(self):        
        server, username, password = self.write_data_before_login()
        self.drclient = DrClientThread(server, username, password)
        self.drclient.start()

当我运行代码时,它给了我例外:

in login_and_keep_alive
self.mydrclient = DrClientThread(server, username, password)
builtins.TypeError: QThread(QObject parent=None): argument 1 has unexpected type 'str'

我做了一些谷歌工作,几乎阅读了与此问题相关的stackoverflow上的每个问题,但我找不到解决方案。实际上,我想这样做:How can I pass arguments to QThread Worker class?

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

class DrClientThread(QtCore.QThread):
    def __int__(self, server, username, password):
        QtCore.QThread.__init__(self)
        self.server = server
        self.username = username
        self.password = password

    def run(self):
        print(self.username, self.password, self.server)

class DrCOM_UI(Ui_Login_Dialog):
    def __init__(self, dialog):
        Ui_Login_Dialog.__init__(self)
        self.setupUi(dialog)
        self.login_btn.clicked.connect(self.login_and_keep_alive)
        ...
    def login_and_keep_alive(self):        
        server, username, password = self.write_data_before_login()
        self.drclient = DrClientThread(self, server, username, password) # <<-----
        self.drclient.start()