# coding=utf8
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Example(QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.initUI() # 主窗口
# 主窗口
def initUI(self):
self.setGeometry(500, 500, 500, 500)
self.setWindowTitle(u"个人基本信息输入框!")
# 前面的标题
self.lb1 = QLabel(u"姓名:", self)
self.lb1.move(20, 10)
self.lb2 = QLabel(u"年龄:", self)
self.lb2.move(20, 60)
self.lb3 = QLabel(u"性别:", self)
self.lb3.move(20, 120)
self.lb4 = QLabel(u"身高(cm):", self)
self.lb4.move(20, 180)
self.lb5 = QLabel(u"基本信息", self)
self.lb5.move(20, 240)
# 中间显示 的内容
self.lb6 = QLabel(u"张三", self)
self.lb6.move(60, 10)
self.lb7 = QLabel("33", self)
self.lb7.move(60, 60)
self.lb8 = QLabel(u"男", self)
self.lb8.move(60, 120)
self.lb9 = QLabel(u"180", self)
self.lb9.move(80, 180)
# 文本框
self.tb = QTextBrowser(self)
self.tb.move(20, 280)
# 按钮
bt_sizeX="120"
bt_sizeY="30"
self.bt1 = QPushButton(u"修改姓名", self)
self.bt1.resize(int(bt_sizeX),int(bt_sizeY))
self.bt1.move(200, 10)
self.bt2 = QPushButton(u"修改年龄", self)
self.bt2.resize(int(bt_sizeX), int(bt_sizeY))
self.bt2.move(200, 60)
self.bt3 = QPushButton(u"修改性别", self)
self.bt3.resize(int(bt_sizeX), int(bt_sizeY))
self.bt3.move(200, 120)
self.bt4 = QPushButton(u"修改身高", self)
self.bt4.resize(int(bt_sizeX), int(bt_sizeY))
self.bt4.move(200, 180)
self.bt5 = QPushButton(u"添加信息到信息框", self)
self.bt5.resize(int(bt_sizeX), int(bt_sizeY))
self.bt5.move(200, 240)
self.show()
# 功能
self.bt1.clicked.connect(self.showDialog)
self.bt2.clicked.connect(self.showDialog)
self.bt3.clicked.connect(self.showDialog)
self.bt4.clicked.connect(self.showDialog)
self.bt5.clicked.connect(self.showDialog)
# 关闭窗口是的提示
def closeEvent(self,QCloseEvent):
reply=QtGui.QMessageBox.question(self,u"警告!",u"确认退出吗?",QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
if reply==QtGui.QMessageBox.Yes:
QCloseEvent.accept()
else:
QCloseEvent.ignore()
def showDialog(self):
sender = self.sender()
sex = [u"男", u"女"]
if sender == self.bt1:
# 输入框
text, ok = QInputDialog.getText(self, u"修改姓名", u"请输入姓名:")
if ok:
self.lb6.setText(text)
elif sender == self.bt2:
# 整数输入
text, ok = QInputDialog.getInt(self, u"修改年龄", u"请输入年龄:")
if ok:
self.lb7.setText(str(text))
elif sender == self.bt3:
# 下拉
text, ok = QInputDialog.getItem(self, u"修改性别", u"请选择性别:", sex)
if ok:
self.lb8.setText(text)
elif sender == self.bt4:
# 小数输入
text, ok = QInputDialog.getDouble(self, u"修改身高", u"请输入身高:", min = 1.0)
if ok:
self.lb9.setText(str(text))
elif sender == self.bt5:
xx=self.lb6.text()+"\n\n"+self.lb7.text()+"\n\n"+self.lb8.text()+"\n\n"+self.lb9.text()
self.tb.setText(unicode(xx))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ui = Example()
ui.show()
sys.exit(app.exec_())
py 基本信息输入框 笔记
最新推荐文章于 2023-07-30 00:03:25 发布