【发布时间】:2015-02-03 10:02:25
【问题描述】:
我有一个 python 控制台脚本,我想向其中添加一个基本状态窗口,所以在对 pyqt 了解不多的情况下,我添加了一个窗口。如果我从我的主线程启动 pyqt,它会阻塞其他所有内容,所以我从另一个线程启动它。几个月来它一直运行良好,但我只是注意到一个警告(不知道我之前是怎么错过的):
WARNING: QApplication was not created in the main() thread.我想知道这可能会导致什么问题。
这是我正在使用的代码的精简版,只是更新了窗口标题栏:
from PyQt4 import QtGui, QtCore
import threading
import sys
from time import sleep
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
self.setWindowTitle(statusLine)
self.timer = QtCore.QBasicTimer()
self.timer.start(500, self)
def updateWindow(self):
self.setWindowTitle(statusLine)
def timerEvent(self, event):
if event.timerId() == self.timer.timerId():
self.updateWindow()
else:
super(MainWidget, self).timerEvent(event)
def startWindow():
app = QtGui.QApplication(sys.argv)
mw = MainWidget()
mw.show()
app.exec_()
if __name__ == '__main__':
global statusLine
statusLine = 'foo'
threadWindow = threading.Thread(target=startWindow)
threadWindow.start()
sleep(2) # process lots of data
statusLine = 'bar'
# keep doing stuff and updating statusLine
编辑:看起来我没有收到这个简化示例的警告;相反,我似乎只有在启动 pyQt 之前启动多个其他 python 线程时才能得到它。但是问题仍然存在:这样做有什么问题?
【问题讨论】:
标签: python multithreading pyqt