【多线程】python界面阻塞,白屏,not responding解决的简单例子

 1 # -*- coding: utf-8 -*-
2
3 import sys, time
4 from PyQt4.QtCore import *
5 from PyQt4.QtGui import *
6 x = 0
7 class Window(QWidget):
8 def __init__(self, parent = None):
9 QWidget.__init__(self, parent)
10 self.thread = Worker()
11
12 # 提示信息
13 self.xLable = QLabel("Number of xTimes:")
14 # 下拉框
15 self.spinBox = QSpinBox()
16 self.spinBox.setMaximum(100)
17 self.spinBox.setValue(10)
18 self.startButton = QPushButton(self.tr("&Start"))
19 # 布局
20 layout = QGridLayout()
21 layout.addWidget(self.xLable, 0, 0)
22 layout.addWidget(self.spinBox, 0, 1)
23 layout.addWidget(self.startButton, 0, 2)
24 self.setLayout(layout)
25 # 标题
26 self.setWindowTitle(self.tr("Threading"))
27
28 # 信号
29 self.connect(self.thread, SIGNAL("finished()"), self.finishSend)
30 self.connect(self.thread, SIGNAL("update(int)"), self.updateGUIStatus)
31 self.connect(self.startButton, SIGNAL("clicked()"), self.sendAdvMail)
32
33 def sendAdvMail(self):
34 self.spinBox.setReadOnly(True)
35 self.startButton.setEnabled(False)
36 #传递值到线程中
37 self.thread.render(self.spinBox.value())
38
39 def updateGUIStatus(self, leftTime):
40 self.xLable.setText(str(leftTime))
41
42 def finishSend(self):
43 self.spinBox.setReadOnly(False)
44 self.startButton.setEnabled(True)
45
46 class Worker(QThread):
47 def __init__(self, parent = None):
48 QThread.__init__(self, parent)
49 self.exiting = False
50 self.xTimes = 0
51
52 def __del__(self):
53 self.exiting = True
54 self.wait()
55
56 def render(self, xTimes):
57 self.xTimes = xTimes
58 self.start()
59
60 def run(self):
61 # Note: This is never called directly. It is called by Qt once the
62 # thread environment has been set up.
63 n = self.xTimes
64 while not self.exiting and n > 0:
65 time.sleep(1)
66 #该信号引起界面更新
67 n -= 1
68 self.emit(SIGNAL("update(int)"), n)
69
70
71 if __name__ == "__main__":
72 app = QApplication(sys.argv)
73 window = Window()
74 window.show()
75 sys.exit(app.exec_())


编辑器加载中...

转载于:https://www.cnblogs.com/txw1958/archive/2011/12/29/2305890.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值