37,719
社区成员
发帖
与我相关
我的任务
分享
class MyThread(QtCore.QThread):
trigger = QtCore.pyqtSignal(str)
def __init__(self,parent=None):
super(MyThread, self).__init__(parent)
def run(self):
# your task code
self.Lable2Action()
self.exec_()
global num2
num2=0
def timeOut2(self):
global num2
#num=num+1
num2=num2+1
if (num2%2==0):
self.trigger.emit("G:/python/icon/shuijing/039/red.png")
else:
self.trigger.emit("G:/python/icon/shuijing/039/grey.png")
#pass
def Lable2Action(self):
self.timer2=QtCore.QTimer()
self.timer2.setInterval(500)
self.timer2.start()
QtCore.QObject.connect(self.timer2,QtCore.SIGNAL("timeout()"),self.timeOut2)
For QTimer to work, you must have an event loop in your application; that is, you must call QCoreApplication::exec() somewhere. Timer events will be delivered only while the event loop is running.
In multithreaded applications, you can use QTimer in any thread that has an event loop. To start an event loop from a non-GUI thread, use QThread::exec(). Qt uses the timer's thread affinity to determine which thread will emit thetimeout() signal. Because of this, you must start and stop the timer in its thread; it is not possible to start a timer from another thread.
https://stackoverflow.com/questions/10492480/starting-qtimer-in-a-qthreadfrom 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("G:/python/icon/shuijing/039/yellow.png")
self.x=QPixmap("G:/python/icon/shuijing/039/yellow.png")
self.thread = MyThread()
self.thread.trigger.connect(self.Lable2Show)
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("G:/python/icon/shuijing/039/red.png")
self.label.setScaledContents(True)
self.label.setPixmap(self.b)
self.label.repaint()
else:
self.b=QPixmap("G:/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 Lable2Show(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("G:/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
self.Lable2Action()
global num2
num2=0
def timeOut2(self):
global num2
#num=num+1
num2=num2+1
if (num%2==0):
self.trigger.emit("G:/python/icon/shuijing/039/red.png")
else:
self.trigger.emit("G:/python/icon/shuijing/039/grey.png")
#pass
def Lable2Action(self):
self.timer2=QtCore.QTimer()
self.timer2.setInterval(500)
self.timer2.start()
QtCore.QObject.connect(self.timer2,QtCore.SIGNAL("timeout()"),self.timeOut2)
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()
@wn0112 帮忙看下我创建的那个线程为什么没有成功
class MyThread(QtCore.QThread):
trigger = QtCore.Signal(str)
def __init__(self,parent=None):
super(MyThread, self).__init__(parent)
def run(self):
num=0
while True:
num += 1
if (num%2==0):
self.trigger.emit("G:/python/icon/shuijing/039/red.png")
else:
self.trigger.emit("G:/python/icon/shuijing/039/grey.png")
time.sleep(0.2)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.button = QPushButton(self, 'Start')
self.button.clicked.connect(self.clickButton)
self.th = MyThread(self)
self.th.finished.connect(self.stopAnimation)
def stopAnimation(self):
pass
def startAnimation(self):
pass
def clickButton(self):
self.startAnimation()
self.th.start()
class MyThread(QThread):
def run(self):
# your task code
pass