【多线程】python界面阻塞,白屏,not responding解决的简单例子
2013-07-23 17:05
666 查看
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_())
编辑器加载中...
相关文章推荐
- 【多线程】python界面阻塞,白屏,not responding解决的简单例子
- 【多线程】python界面阻塞,白屏,not responding解决的简单例子
- 【多线程】python界面阻塞,白屏,not responding解决的简单例子
- Python3.5 多线程简单例子--threading.Thread
- python多线程简单例子
- python多线程简单例子
- Python多线程简单例子
- Python 多线程简单例子
- python QT chapter 1 简单的例子显示界面
- python多线程 简单例子
- Python中多线程的阻塞问题
- 最简单解决 Ubuntu 16.* 在基于 Qt 为界面的程序中无法输入中文(Latex\Pycharm)
- 安装 Python 相关组件导致 VNC 界面黑屏的解决办法
- 学习简单的python----最简单的python+GUI例子+2(带排版的GUI)
- python 简单的多线程链接(二)
- 多线程中让main方法最先开始,最后结束的简单例子
- python 多线程就这么简单(续)
- python 多线程就这么简单
- 最近用htmlunit做网络爬虫 遇到拿不到初始化js加载的数据的问题 最近解决了 写个简单的例子
- python hard way中的一个小例子,简单的游戏引擎