1 # -*- coding: utf-8 -*- 2 from PyQt4.QtGui import * 3 from PyQt4.QtCore import * 4 import sys 5 6 QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8")) 7 8 class Progess(QDialog): 9 def __init__(self,parent=None): 10 super(Progess,self).__init__(parent) 11 self.setWindowTitle(self.tr("使用进度条")) 12 numLabel=QLabel(self.tr("文件数目")) 13 self.numLineEdit=QLineEdit("10") 14 typeLabel=QLabel(self.tr("显示类型")) 15 self.typeComboBox=QComboBox() 16 self.typeComboBox.addItem(self.tr("进度条")) 17 self.typeComboBox.addItem(self.tr("进度对话框")) 18 19 self.progressBar=QProgressBar() 20 21 startPushButton=QPushButton(self.tr("开始")) 22 23 layout=QGridLayout() 24 layout.addWidget(numLabel,0,0) 25 layout.addWidget(self.numLineEdit,0,1) 26 layout.addWidget(typeLabel,1,0) 27 layout.addWidget(self.typeComboBox,1,1) 28 layout.addWidget(self.progressBar,2,0,1,2) 29 layout.addWidget(startPushButton,3,1) 30 layout.setMargin(15) 31 layout.setSpacing(10) 32 33 self.setLayout(layout) 34 35 self.connect(startPushButton,SIGNAL("clicked()"),self.slotStart) 36 37 def slotStart(self): 38 num=int(self.numLineEdit.text()) 39 40 if self.typeComboBox.currentIndex()==0: 41 self.progressBar.setMinimum(0) 42 self.progressBar.setMaximum(num) 43 44 for i in range(num): 45 self.progressBar.setValue(i) 46 QThread.msleep(100) 47 48 elif self.typeComboBox.currentIndex()==1: 49 progressDialog=QProgressDialog(self) 50 progressDialog.setWindowModality(Qt.WindowModal) 51 progressDialog.setMinimumDuration(5) 52 progressDialog.setWindowTitle(self.tr("请等待")) 53 progressDialog.setLabelText(self.tr("拷贝...")) 54 progressDialog.setCancelButtonText(self.tr("取消")) 55 progressDialog.setRange(0,num) 56 57 for i in range(num): 58 progressDialog.setValue(i) 59 QThread.msleep(100) 60 if progressDialog.wasCanceled(): 61 return 62 63 app=QApplication(sys.argv) 64 progess=Progess() 65 progess.show() 66 app.exec_()