加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – PyQt 4 UI冻结

发布时间:2020-12-20 12:16:03 所属栏目:Python 来源:网络整理
导读:以下程序应该只计数和int并在标签中显示其值. 但过了一会儿,GUI停止工作,而循环继续. from PyQt4 import QtGui,QtCoreimport sysclass main_window(QtGui.QWidget): def __init__(self,parent=None): #Layout QtGui.QWidget.__init__(self,parent) self.bt=Q
以下程序应该只计数和int并在标签中显示其值.
但过了一会儿,GUI停止工作,而循环继续.

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 =假.
GUI可能会停止工作,因为它没有足够的时间来执行重绘.您可能希望在循环中添加一些time.sleep以等待GUI重新绘制.

更新:您应该使用QTimer而不是简单的while循环来实现您正在实施的行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读