【问题标题】:pyqt QThread and Signal and Emit not working for this answerpyqt QThread 和 Signal and Emit 不适用于此答案
【发布时间】:2016-08-09 20:31:25
【问题描述】:

所以我一直在寻找如何使用 QThreads 和 Signals 的答案,并从以下位置找到答案:

How to access GUI elements from another thread in PyQt

想知道这是否对其他人不起作用?窗口只是冻结。我的电脑有什么问题吗?还是答案?

代码是:

from PyQt4 import QtGui as gui
from PyQt4 import QtCore as core

import sys
import time


class ServerThread(core.QThread):
    def __init__(self, parent=None):
        core.QThread.__init__(self)

    def start_server(self):
        for i in range(1,6):
            time.sleep(1)
            self.emit(core.SIGNAL("dosomething(QString)"), str(i))

    def run(self):
        self.start_server()


class MainApp(gui.QWidget):
    def __init__(self, parent=None):
        super(MainApp,self).__init__(parent)

        self.label = gui.QLabel("hello world!!")

        layout = gui.QHBoxLayout(self)
        layout.addWidget(self.label)

        self.thread = ServerThread()
        self.thread.start()

        self.connect(self.thread, core.SIGNAL("dosomething(QString)"), self.doing)

    def doing(self, i):
        self.label.setText(i)
        if i == "5":
            self.destroy(self, destroyWindow =True, destroySubWindows = True)
            sys.exit()


app = gui.QApplication(sys.argv)
form = MainApp()
form.show()
app.exec_()

【问题讨论】:

    标签: python pyqt


    【解决方案1】:

    该代码适用于我Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)] on win32。不过,该 sn-p 中有一个小错误:

    你应该把这行self.destroy(self, destroyWindow =True, destroySubWindows = True)改成self.destroy(destroyWindow =True, destroySubWindows = True),否则你会得到这个错误

    TypeError: QWidget.destroy(bool destroyWindow=True, bool destroySubWindows=True): 参数 1 具有意外类型“MainApp”

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多