PyQt 4 UI 冻结 [英] PyQt 4 UI freezes
本文介绍了PyQt 4 UI 冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下程序应该只是计数和 int 并在标签中显示其值.但过了一会儿,GUI 停止工作,而循环继续.
The following programm should just count up and int and displays its value in a label. But after a while the GUI stops working, while the loop continous.
from PyQt4 import QtGui,QtCore
import sys
class main_window(QtGui.QWidget):
def __init__(self,parent=None):
#Layout
QtGui.QWidget.__init__(self,parent)
self.bt=QtGui.QPushButton('crash')
self.lbl=QtGui.QLabel('count')
ver=QtGui.QHBoxLayout(self)
ver.addWidget(self.bt)
ver.addWidget(self.lbl)
self.cnt=0
self.running=False
self.connect(self.bt,QtCore.SIGNAL("clicked()"),self.count)
def count(self):
self.running=True
while self.running:
self.cnt+=1
print self.cnt
self.lbl.setText(str(self.cnt))
self.repaint()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mw=main_window()
mw.show()
sys.exit(app.exec_())
有什么帮助吗?
推荐答案
def count(self):
self.running=True
while self.running:
self.cnt+=1
print self.cnt
self.lbl.setText(str(self.cnt))
self.repaint()
你有没有想过退出这个无限循环?例如.self.running=False
.
GUI 可能会停止工作,因为它没有足够的时间来执行 repaint
.您可能希望在循环中添加一些 time.sleep
以等待 GUI 重新绘制.
Have you thought about any exit from this endless loop? E.g. self.running=False
.
GUI may stop working because it doesn't have enough time to perform repaint
. You may want to add some time.sleep
in the loop to wait for the GUI to repaint.
更新:您应该使用 QTimer,不是简单的 while 循环,适用于您正在实施的行为.
Upd.: You should use QTimer, not a simple while loop, for the behavior you're implementing.
这篇关于PyQt 4 UI 冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文