用PyQt写的图片播放器。
用线程控制。
线程之间的信号传递。
1 # -*- coding:utf-8 -*- 2 3 from PyQt4 import QtGui, QtCore 4 import sys, os, time 5 6 # from datetime import date, time, datetime, timedelta 7 QtCore.QTextCodec.setCodecForTr(QtCore.QTextCodec.codecForName("utf8")) 8 9 GetMyPath = [] 10 class Image_view(QtGui.QMainWindow): 11 def __init__(self): 12 super(Image_view, self).__init__() 13 14 self.setWindowTitle(self.tr("Image_view")) 15 self.resize(800, 450) 16 17 self.imageLabel = QtGui.QLabel() 18 self.imageLabel.setSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Ignored) 19 self.imageLabel.setScaledContents(True) 20 self.setCentralWidget(self.imageLabel) 21 22 self.createActions() 23 self.createButton() 24 self.MyImage = QtGui.QImage() 25 26 self.th_cmd = mythread() 27 self.th_cmd._signal.connect(self.runImages) 28 29 def createActions(self): 30 self.openFileAction = QtGui.QAction(self.tr("Open Images Path"), self) 31 self.connect(self.openFileAction, QtCore.SIGNAL("triggered()"), self.OpenFile) 32 33 self.Play_Image = QtGui.QAction(self.tr("Play"), self) 34 self.connect(self.Play_Image, QtCore.SIGNAL("triggered()"), self.StartPlay) 35 36 self.Stop_Image = QtGui.QAction(self.tr("Stop"), self) 37 self.connect(self.Stop_Image, QtCore.SIGNAL("triggered()"), self.StopPay) 38 39 def createButton(self): 40 fileTool = self.addToolBar(self.tr("文件")) 41 fileTool.addAction(self.openFileAction) 42 43 Play_Image_Tool = self.addToolBar(self.tr("播放")) 44 Play_Image_Tool.addAction(self.Play_Image) 45 46 Exit_Image_Tool = self.addToolBar(self.tr("停止")) 47 Exit_Image_Tool.addAction(self.Stop_Image) 48 49 def OpenFile(self): 50 s = QtGui.QFileDialog.getExistingDirectory(self, u"Give Me FileName[填入路径文件名]:") 51 for i in os.listdir(s): 52 GetMyPath.append(str(s) + "\" + str(i)) 53 #print GetMyPath 54 55 def StartPlay(self): 56 self.th_cmd.start() 57 58 def StopPay(self): 59 self.th_cmd.terminate() # terminate 终止进程. 60 61 def runImages(self, item): 62 MyPath = GetMyPath 63 if self.MyImage.load(MyPath[int(item)]) is not None: 64 self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(self.MyImage)) 65 66 67 class mythread(QtCore.QThread): 68 _signal = QtCore.pyqtSignal(str) 69 70 def __init__(self): 71 super(mythread, self).__init__() 72 73 def run(self, day=0, hour=0, min=0, second=2): 74 for i in range(len(GetMyPath)): 75 self._signal.emit(str(i)) 76 77 time.sleep(0.1) 78 79 80 81 if __name__ == "__main__": 82 app = QtGui.QApplication(sys.argv) 83 win = Image_view() 84 win.show() 85 app.exec_()