PyQT4:从并行线程中的另一个python模块获取值

2024-04-23 20:04:53 发布

您现在位置:Python中文网/ 问答频道 /正文

PyQT和QThread有问题。 在QThread run()中使用另一个模块的方法时,无法从中获取值。 我怎样才能得到它们?你知道吗

from PyQt4 import QtCore, QtGui
import sys


class MainWindow(QtGui.QMainWindow):
def __init__(self):

    QtGui.QMainWindow.__init__(self)
    self.initAnotherThread()
    self.setFixedSize(640, 360)
    self.setWindowTitle('Window')

    self.logText = QtGui.QTextEdit(self)
    self.logText.setReadOnly(True)
    self.logText.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
    self.logText.resize(460, 330)

    self.clickButton = QtGui.QPushButton('Click Button', self)
    self.clickButton.setFocusPolicy(QtCore.Qt.NoFocus)
    self.clickButton.setGeometry(470, 50, 160, 35)
    self.connect(self.clickButton, QtCore.SIGNAL('clicked()'), self.onButtonClick)

def initAnotherThread(self):
    self.updaterThread = anotherThread(self)

def onButtonClick(self):
    self.logText.clear()
    self.updaterThread.start()

class anotherThread(QtCore.QThread):
    def __init__(self, mw):
        super(anotherThread, self).__init__(mw)
        self.mw = mw

    def run(self):
        print "Another thread started"
        AnotherModule.anotherMethod()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

在另一个模块中,我有下一个:

anotherMethod():
    x=0
    while x<100:
        x+=1

我应该怎么做才能从另一个模块得到x值?你知道吗


Tags: 模块runimportselfinitmaindefsys