lason123 |
2017-07-23 15:45 |
pyqt4 多线程定时器为什么没起效
我用pyqt4做了个界面,两个label(label和label1),程序目的:点击start按钮,第一个label上会交替显示图片,用定时器来实现,第二个label1用了多线程的办法,点击mythread按钮,也想让它用定时器实现图片的交替显示,但没成功,反应都没有,为什么主线程里可以用定时器,自己创建的线程里(红色部分)为什么不能用定时器,请指教,谢谢!
# -*- coding: utf-8 -*- from PyQt4.QtGui import QApplication, QLabel, QMovie, QPainter, QFontMetrics,QPixmap import time from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object): def __init__(self): #super(Ui_Dialog, self).__init__() self.m=QPixmap("I:/python/icon/shuijing/039/yellow.png") self.x=QPixmap("I:/python/icon/shuijing/039/yellow.png") self.thread = MyThread() self.thread.trigger.connect(self.Lable1Show) def setupUi(self, Dialog): Dialog.setObjectName(_fromUtf8("Dialog")) Dialog.resize(700, 600) self.label = QLabel(Dialog) self.label.setGeometry(QtCore.QRect(80, 50, 200, 200)) self.label.setObjectName(_fromUtf8("label")) self.label1 = QLabel(Dialog) self.label1.setGeometry(QtCore.QRect(280, 50, 200, 200)) self.label1.setObjectName(_fromUtf8("label1")) self.pushButton = QtGui.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(170, 300, 191, 61)) self.pushButton.setText('Stop') self.pushButton.setObjectName(_fromUtf8("pushButton")) self.pushButton2 = QtGui.QPushButton(Dialog) self.pushButton2.setGeometry(QtCore.QRect(170, 400, 191, 61)) self.pushButton2.setText('myThread') self.pushButton2.setObjectName(_fromUtf8("pushButton2")) self.pushButton3 = QtGui.QPushButton(Dialog) self.pushButton3.setGeometry(QtCore.QRect(170, 500, 191, 61)) self.pushButton3.setText('start') self.pushButton3.setObjectName(_fromUtf8("pushButton3")) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")),self.stop1) QtCore.QObject.connect(self.pushButton2, QtCore.SIGNAL(_fromUtf8("clicked()")),self.myThread) QtCore.QObject.connect(self.pushButton3, QtCore.SIGNAL(_fromUtf8("clicked()")),self.start) #QtCore.QObject.connect(self.pushButton2, QtCore.SIGNAL(_fromUtf8("clicked()")),self.changePic2) #self.label.setMovie(self.m) self.label.setScaledContents(True) self.label.setPixmap(self.m) self.label1.setScaledContents(True) self.label1.setPixmap(self.x) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def start(self): self.timer=QtCore.QTimer() self.timer.setInterval(500) self.timer.start() QtCore.QObject.connect(self.timer,QtCore.SIGNAL("timeout()"),self.timeOut) global num num=0 def timeOut(self): global num #num=num+1 num=num+1 if (num%2==0): self.b=QPixmap("I:/python/icon/shuijing/039/red.png") self.label.setScaledContents(True) self.label.setPixmap(self.b) self.label.repaint() else: self.b=QPixmap("I:/python/icon/shuijing/039/green.png") self.label.setScaledContents(True) self.label.setPixmap(self.b) self.label.repaint() def myThread(self): self.thread.start() def Lable1Show(self,str1): #self.b=QPixmap("I:/python/icon/shuijing/039/red.png") self.h=QPixmap(str1) self.label1.setScaledContents(True) self.label1.setPixmap(self.h) self.label1.repaint() def stop1(self): #flag=0 self.timer.stop() self.b=QPixmap("I:/python/icon/shuijing/039/green.png") self.label.setScaledContents(True) self.label.setPixmap(self.b) self.thread.terminate() def retranslateUi(self, Dialog): Dialog.setWindowTitle(_translate("Dialog", "Dialog", None)) class MyThread(QtCore.QThread): trigger = QtCore.pyqtSignal(str) def __init__(self,parent=None): super(MyThread, self).__init__(parent) def run(self): # your task code while True: self.timer2=QtCore.QTimer() self.timer2.setInterval(500) self.timer2.start() QtCore.QObject.connect(self.timer2,QtCore.SIGNAL("timeout()"),self.timeOut2) def timeOut2(self): num=0 while True: num += 1 if (num%2==0): self.trigger.emit("I:/python/icon/shuijing/039/red.png") else: self.trigger.emit("I:/python/icon/shuijing/039/grey.png") #time.sleep(1) def main(): app = QtGui.QApplication(sys.argv) Dialog = QtGui.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_()) if __name__ == "__main__": import sys main() |
|