码迷,mamicode.com
首页 > 其他好文 > 详细

使用标准输入框

时间:2016-06-21 01:04:18      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

 1 # -*- coding: utf-8 -*-   
 2 from PyQt4.QtCore import *  
 3 from PyQt4.QtGui import *  
 4 import sys  
 5   
 6 QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))  
 7   
 8 class InputDlg(QDialog):  
 9     def __init__(self,parent=None):  
10         super(InputDlg,self).__init__(parent)  
11   
12         label1=QLabel(self.tr("姓名"))  
13         label2=QLabel(self.tr("性别"))  
14         label3=QLabel(self.tr("年龄"))  
15         label4=QLabel(self.tr("身高"))  
16   
17         self.nameLabel=QLabel("TengWei")  
18         self.nameLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken)  
19         self.sexLabel=QLabel(self.tr(""))  
20         self.sexLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken)  
21         self.ageLabel=QLabel("25")  
22         self.ageLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken)  
23         self.statureLabel=QLabel("168")  
24         self.statureLabel.setFrameStyle(QFrame.Panel|QFrame.Sunken)  
25   
26         nameButton=QPushButton("...")  
27         sexButton=QPushButton("...")  
28         ageButton=QPushButton("...")  
29         statureButton=QPushButton("...")  
30   
31         self.connect(nameButton,SIGNAL("clicked()"),self.slotName)  
32         self.connect(sexButton,SIGNAL("clicked()"),self.slotSex)  
33         self.connect(ageButton,SIGNAL("clicked()"),self.slotAge)  
34         self.connect(statureButton,SIGNAL("clicked()"),self.slotStature)  
35   
36         layout=QGridLayout()  
37         layout.addWidget(label1,0,0)  
38         layout.addWidget(self.nameLabel,0,1)  
39         layout.addWidget(nameButton,0,2)  
40         layout.addWidget(label2,1,0)  
41         layout.addWidget(self.sexLabel,1,1)  
42         layout.addWidget(sexButton,1,2)  
43         layout.addWidget(label3,2,0)  
44         layout.addWidget(self.ageLabel,2,1)  
45         layout.addWidget(ageButton,2,2)  
46         layout.addWidget(label4,3,0)  
47         layout.addWidget(self.statureLabel,3,1)  
48         layout.addWidget(statureButton,3,2)  
49   
50         self.setLayout(layout)  
51   
52         self.setWindowTitle(self.tr("资料收集"))  
53   
54     def slotName(self):  
55         name,ok=QInputDialog.getText(self,self.tr("用户名"),  
56                                      self.tr("请输入新的名字:"),  
57                                      QLineEdit.Normal,self.nameLabel.text())  
58         if ok and (not name.isEmpty()):  
59             self.nameLabel.setText(name)  
60   
61     def slotSex(self):  
62         list=QStringList()  
63         list.append(self.tr(""))  
64         list.append(self.tr(""))  
65         sex,ok=QInputDialog.getItem(self,self.tr("性别"),self.tr("请选择性别"),list)  
66   
67         if ok:  
68             self.sexLabel.setText(sex)  
69   
70     def slotAge(self):  
71         age,ok=QInputDialog.getInteger(self,self.tr("年龄"),  
72                                        self.tr("请输入年龄:"),  
73                                        int(self.ageLabel.text()),0,150)  
74         if ok:  
75             self.ageLabel.setText(str(age))  
76   
77     def slotStature(self):  
78         stature,ok=QInputDialog.getDouble(self,self.tr("身高"),  
79                                           self.tr("请输入身高:"),  
80                                           float(self.statureLabel.text()),0,2300.00)  
81         if ok:  
82             self.statureLabel.setText(str(stature))  
83   
84 app=QApplication(sys.argv)  
85 form=InputDlg()  
86 form.show()  
87 app.exec_()  

 

View Code

 

使用标准输入框

标签:

原文地址:http://www.cnblogs.com/test404/p/5602203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!