首页 > 代码库 > pyqt QTimer,QThread例子学习
pyqt QTimer,QThread例子学习
# -*- coding: utf-8 -*-
# python:2.x
__author__ = ‘Administrator‘
from PyQt4.QtGui import *
from PyQt4.Qt import *
from PyQt4.QtCore import *
from start import Ui_Form
import sys
class Example(QDialog,Ui_Form):
def __init__(self,parent=None):
super(Example, self).__init__(parent)
self.setupUi(self)
#边框风格
self.timenum.setFrameStyle(QFrame.StyledPanel|
QFrame.Plain)
self.time=QTimers()
self.start.clicked.connect(self.start1)
self.stop.clicked.connect(self.stop1)
self.connect(self.time, SIGNAL("updateTime()")
, self.updateTime)
self.setWindowFlags(Qt.WindowMinimizeButtonHint)#禁止改变窗口的大小
def updateTime(self):
self.timenum.setText(u‘时间为{0}‘.format(QString.number(self.num)))
self.num+=1
def stop1(self):
self.time.stop1()
self.stop.setEnabled(False)
self.start.setEnabled(True)
self.timenum.setText(u‘请输入时间0秒‘)
def start1(self):
self.num=0
self.start.setEnabled(False)
self.stop.setEnabled(True)
self.time.start()
from time import *
class QTimers(QThread):
def __init__(self, parent=None):
super(QTimers,self).__init__(parent)
self.stoped=False
self.mutex=QMutex()#提供的是线程之间的访问顺序化
def run(self):
with QMutexLocker(self.mutex):#QMutexLocker ( QMutex * mutex )
self.stoped=False
while True:
if self.stoped:
return
self.emit(SIGNAL(‘updateTime()‘))#发送信号
sleep(1)
def stop1(self):
with QMutexLocker(self.mutex):
self.stoped=True
def isStoped(self):
with QMutexLocker(self.mutex):
return self.stoped
app =QApplication(sys.argv)
QApplication.setQuitOnLastWindowClosed(False)
x = Example()
x.show()
sys.exit(app.exec_())
如图: