Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2842974
  • 博文数量: 348
  • 博客积分: 2907
  • 博客等级: 中校
  • 技术积分: 2272
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-12 09:16
个人简介

专注 K8S研究

文章分类

全部博文(348)

文章存档

2019年(22)

2018年(57)

2016年(2)

2015年(27)

2014年(33)

2013年(190)

2011年(3)

2010年(14)

分类: Python/Ruby

2013-09-02 22:38:54

主窗口上有个按钮(QPushButton),按一下则调用槽函数doSomething(),doSomething()里有个for循环,每循环一次time.sleep(10)。结果就是点了按钮以后,程序进入for循环,主窗口卡死。
解决方法,使用Timer:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class TestWindow(QDialog):
    def __init__(self):
        super(TestWindow,self).__init__()

        self.currentLoopNum=0

        self.testPushBotton=QPushButton(self)
        self.testPushBotton.setText("Start")
        self.stopPushBotton=QPushButton(self)
        self.stopPushBotton.setText("Stop")

        self.testLabel=QLabel(self)

        self.testLayout=QGridLayout(self)
        self.testLayout.addWidget(self.testPushBotton,0,0)
        self.testLayout.addWidget(self.stopPushBotton,0,1)
        self.testLayout.addWidget(self.testLabel,1,0,1,2)

        self.testTimer=QTimer(self)

        self.connect(self.testPushBotton,SIGNAL("clicked()"),self.doSomething)
        self.connect(self.testTimer,SIGNAL("timeout()"),self.timeOut)
        self.connect(self.stopPushBotton,SIGNAL("clicked()"),self.stopTimer)

    def doSomething(self):
        self.testTimer.start(1000)

    def timeOut(self):
        self.currentLoopNum+=1
        self.testLabel.setText(str(self.currentLoopNum))

    def stopTimer(self):
        self.testTimer.stop()

app=QApplication(sys.argv)
form=TestWindow()
form.show()
app.exec_()
阅读(11281) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~