python多线程 不在main_从python线程(不是main)启动pyQt线程有什么不...
我有一個想要向其添加基本狀態(tài)窗口的python控制臺腳本,因此在不了解pyqt的情況下,我添加了一個窗口.如果我從主線程啟動pyqt,它將阻塞其他所有內(nèi)容,因此我從另一個線程啟動了它.這樣的運行情況已經(jīng)好幾個月了,但我只是注意到了一個警告(不確定我以前怎么錯過它):
警告:QApplication不是在main()線程中創(chuàng)建的.我想知道這可能會導(dǎo)致什么問題.
這是我使用的代碼的精簡版,僅更新窗口標(biāo)題欄:
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線程時才得到它.但是問題仍然存在:這樣做有什么問題?
總結(jié)
以上是生活随笔為你收集整理的python多线程 不在main_从python线程(不是main)启动pyQt线程有什么不...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql not exists用法_SQL
- 下一篇: 小甲鱼零基础入门python二十一课课后