首页 > 代码库 > PyQt4学习笔记2014-11-25

PyQt4学习笔记2014-11-25

Prentice.Hall.Rapid.GUI.Programming.with.Python.and.Qt.the.definitive.guide.to.PyQt.programming.2008.pdf 里面的例子实现。

 

第四章全部例子:window测试通过

图片和代码的顺序不是一致的,不过应该很容易看出来,然后有些例子做了一些改动,比如下面第一张图。

 

 

 

 

 

  1 import sys  2 import time  3 from PyQt4.QtCore import *  4 from PyQt4.QtGui import *  5   6 app=QApplication(sys.argv)  7 try:  8       9     due=QTime.currentTime() 10     message=Alter 11     if len(sys.argv) <2: 12     raise ValueError 13     hours,mins = sys.argv[1].split(":") 14     due=QTime(int(hours),int(mins)) 15     if len(sys.argv) >=2: 16     message = ‘‘.join(sys.argv[2:]) 17 except ValueError: 18     message = Usage: alter.pyw HH:MM 19 while QTime.currentTime() <= due: 20     time.sleep(20) 21 label = QLabel(<font color=red size=72 ><b>message</b></font>) 22 label.setWindowFlags(Qt.SplashScreen) 23 label.show() 24 QTimer.singleShot(6000,app.quit) 25 app.exec_() 26  27 ------------------------------------------------------------------------- 28  29 # -*- coding: utf-8 -*- 30 from __future__ import division #引入真实除和数学的相关包 31 import sys 32 from math import * 33 from PyQt4.QtCore import * 34 from PyQt4.QtGui import * 35  36 class Form(QDialog): 37     def __init__(self,parent=None): 38         super(Form,self).__init__(parent) 39         self.setWindowTitle("TextBrowser") 40         self.textbrowser = QTextBrowser()#QTextBrowser()只读的多行的文本框 41         self.lineedit = QLineEdit("input your expression and press enter") 42         self.lineedit.selectAll()#选中全部,注意里面没有参数,下面的setFocus()也没有参数 43         self.lineedit.setFocus() 44         layout = QVBoxLayout() 45         layout.addWidget(self.textbrowser) 46         layout.addWidget(self.lineedit)#要加self 47         self.setLayout(layout) 48         self.connect(self.lineedit,SIGNAL(returnPressed()),self.lineeidt_change)#只写方法名,不要写括号 49         #主要是要记得写self,总是忘记,这里有一个新的内置的signal:returnPressed() 50     def lineeidt_change(self):  51         try: 52             text = unicode(self.lineedit.text()) 53             #self.lineedit.text()获得文本内容 54             self.textbrowser.append("%s = %s" % (text,eval(text)))#使用append方法进行追加 55         except: 56             self.textbrowser.append("<font color=red size =20>%s Error </font>" % str(text)) #格式化符号写在外面,<font>要写在引号里面 57 app = QApplication(sys.argv) 58 form = Form() 59 form.show() 60 app.exec_() #这个是app的结束,不是sys. 61          62 -------------------------------------------------------------------------------- 63 import sys 64 from PyQt4.QtCore import * 65 from PyQt4.QtGui import * 66 from datetime import date 67  68 class Form(QDialog): 69     def __init__(self,parent=None): 70         super(Form,self).__init__(parent) 71         self.mdict = {a:10 ,b:20, c:30} 72         self.setWindowTitle("windows") 73         self.label_date = QLabel(date.taday().strfTime("%Y-%m-%d")) 74         self.comboBox1 = QComboBox() 75         self.comboBox1.addItems(sorted(self.mdict.keys())) 76         self.comboBox2 = QComboBox() 77         self.comboBox2.addItems(sorted(self.mdict.keys())) 78         self.doubleSpinBox = QDoubleSpinBox() 79         self.doubleSpinBox.setRange(0.1,100000) 80         self.doubleSpinBox.setValue(1.0) 81         self.label = QLabel("1.0") 82  83         slef.connect(self.comboBox1,SIGNAL(currentIndexChanged(int)),self.uichange) 84         slef.connect(self.comboBox2,SIGNAL(currentIndexChanged(int)),self.uichange) 85         slef.connect(self.doubleSpinBox,SIGNAL(ValueChanged(double)),self.uichange) 86  87         layout =QGirdLayout() 88         layout.addWidget(self.lable_date,0,0) 89         layout.addWidget(self.comboBox1,1,0) 90         layout.addWidget(self.comboBox2,1,1) 91         layout.addWidget(self.doubleSpinBox,2,0) 92         layout.addWidget(self.label,2,1) 93  94         self.setLayout(layout) 95     def uichange(self): 96         text_b1 = unicode(self.comboBox1.currentText()) 97         text_b2 = unicode(self.comboBox2.currentText()) 98         try: 99             self.label.setText(self.mdict[text_b1] * self.mdict(text_b2) * self.doubleSpinBox.value())100         except:101             102             self.label.setText("<font color=red size = 20><b> ERROR</b></font>")103 app = QApplication(sys.argv)104 form = Form()105 form.show()106 app.exec_()107         108         109         110 ----------------------------------------------------------------------111 # -*- coding: utf-8 -*-112 import sys113 from PyQt4.QtCore import *114 from PyQt4.QtGui import *115 from datetime import datetime116 117 class Form(QDialog):118     def __init__(self,parent=None):119         super(Form,self).__init__(parent)120         self.mdict = {a:10 ,b:20, c:30}121         self.setWindowTitle("windows")122         self.label_date = QLabel(datetime.now().strftime("%Y-%m-%d"))#使用了strftime()方法进行了格式化,从datetime.datetime.now()得到现在的时间123         self.comboBox1 = QComboBox() # QComboBox 下拉框,采用addItems()的方法增加下拉项,可以方便的使用字典的形式。currentText()方法得到现在的下拉项。124         self.comboBox1.addItems(sorted(self.mdict.keys()))#sorted()复制字典的键125         self.comboBox2 = QComboBox()126         self.comboBox2.addItems(sorted(self.mdict.keys()))127         self.doubleSpinBox = QDoubleSpinBox() #128         self.doubleSpinBox.setRange(0.1,100000) # 0.1-100000范围,然后步长是1129         self.doubleSpinBox.setValue(1.0) # 设置值130         self.label = QLabel("1.0")131         self.resize(350,200)132 133         self.connect(self.comboBox1,SIGNAL(currentIndexChanged(int)),self.uichange) #特有的方法: currentIndexChanged(int) and valueChanged(double) 这有一些规律,比如首字母小写,驼峰记法,后面一般都是被动式加括号,里面有相应参数134         self.connect(self.comboBox2,SIGNAL(currentIndexChanged(int)),self.uichange)135         self.connect(self.doubleSpinBox,SIGNAL(valueChanged(double)),self.uichange)136 137         layout = QGridLayout()138         layout.addWidget(self.label_date,0,0) # 添加组件的时候,要记得是绑定调用,即要用self实现对组件的调用,在gui中几乎所有的都要使用self显性的调用。139         layout.addWidget(self.comboBox1,1,0)140         layout.addWidget(self.comboBox2,1,1)141         layout.addWidget(self.doubleSpinBox,2,0)142         layout.addWidget(self.label,2,1)143 144         self.setLayout(layout)145     def uichange(self):146         text_b1 = unicode(self.comboBox1.currentText())147         text_b2 = unicode(self.comboBox2.currentText())148         try:149             a= self.mdict[text_b1]150             b= self.mdict[text_b2]151             print -----------152             c= self.doubleSpinBox.value() #得到doubleSpinBpx的值153             154             amount = a*b*c155             print amount156             self.label.setText(%s% amount)157         except:       158             self.label.setText("<font color=red size = 20><b> ERROR</b></font>")159 app = QApplication(sys.argv)160 form = Form()161 form.show()162 app.exec_()163         164         165         166 --------------------------------------------------------------------------167 import sys168 from PyQt4.QtGui import *169 from PyQt4.QtCore import *170 171 class Form2(QDialog):172     def __init__(self,parent=None):173         super(Form2,self).__init__(parent)174         self.setWindowTitle("Test 2")175         self.dial = QDial()176         self.dial.setNotchesVisible(True)177         self.spinbox = QSpinBox()178 179         layout = QHBoxLayout()180         layout.addWidget(self.dial)181         layout.addWidget(self.spinbox)182 183         self.setLayout(layout)184 185         self.connect(self.dial,SIGNAL(valueChanged(int)),self.spinbox.setValue)186         self.connect(self.spinbox,SIGNAL(valuechanged(int)),self.dial.setValue)187         188 app = QApplication(sys.argv)189 form = Form2()190 form.show()191 app.exec_()192         193 -----------------------------------------------------------------------------194 import sys195 from PyQt4.QtGui import *196 from PyQt4.QtCore import *197 198 class Form3(QSpinBox):199     def __init__(self):200         self.times=0201         super(Form3,self).__init__()202         self.connect(self,SIGNAL(valueChanged(int)),self.checkZero)203         self.connect(self,SIGNAL(zeroTimes),self.func)204         self.resize(150,50)205     def checkZero(self):206         if self.value()==0:            207             self.times+=1208             self.emit(SIGNAL(zeroTimes),self.times)209                 210     def func(self,times):211         print %s times % times212     213 app = QApplication(sys.argv)214 form = Form3()215 form.show()216 app.exec_()217         218 --------------------------------------------------------------------------------219 import sys220 from PyQt4.QtGui import *221 from PyQt4.QtCore import *222 223 class Form4(QDialog):224     def __init__(self):225         super(Form4,self).__init__()226         names=[one,two,three,four,five,six]227         self.button1 = QPushButton(names[0])228         self.button2 = QPushButton(names[1])229         self.button3 = QPushButton(names[2])230         self.button4 = QPushButton(names[3])231         self.button5 = QPushButton(names[4])232         self.button6 = QPushButton(names[5])233 234         layout = QHBoxLayout()235         layout.addWidget(self.button1)236         layout.addWidget(self.button2)237         layout.addWidget(self.button3)238         layout.addWidget(self.button4)239         layout.addWidget(self.button5)240         layout.addWidget(self.button6)241         self.setLayout(layout)242 243         self.connect(self.button1,SIGNAL(clicked()),lambda x=names[0] : self.myFunc(x))244         self.connect(self.button2,SIGNAL(clicked()),lambda x=names[1] : self.myFunc(x))245         self.connect(self.button3,SIGNAL(clicked()),lambda x=names[2] : self.myFunc(x))246         self.connect(self.button4,SIGNAL(clicked()),self.myClicked)247         self.connect(self.button5,SIGNAL(clicked()),self.myClicked)248         self.connect(self.button6,SIGNAL(clicked()),self.myClicked)249     def myClicked(self):250         button = self.sender()251         #if button is None or not isinstance(button,QPushButton):252         if button is None or not hasattr(button,text):253             return254         print the button clicked is button %s % button.text()255 256 257     def myFunc(self,name):258         print the button clicked is button %s % name259         260         261         262 263     264 app = QApplication(sys.argv)265 form = Form4()266 form.show()267 app.exec_()268         269 -----------------------------------------------------------------------------------

 

PyQt4学习笔记2014-11-25