我有一个想要向其添加基本状态窗口的python控制台脚本,因此在不了解pyqt的情况下,我添加了一个窗口.如果我从主线程启动pyqt,它将阻塞其他所有内容,因此我从另一个线程启动了它.这样的运行情况已经好几个月了,但我只是注意到了一个警告(不确定我以前怎么错过它):
警告:QApplication不是在main()线程中创建的.我想知道这可能会导致什么问题.
这是我使用的代码的精简版,仅更新窗口标题栏:
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线程时才得到它.但是问题仍然存在:这样做有什么问题?