这里我们不妨找一个例子做说明,假设我们现在要做一个登录个人信息平台的界面如下:
其中输入的内容可应用到程序中去。那么我们该如何进行呢?步骤如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(357, 190)
self.widget = QtGui.QWidget(Dialog)
self.widget.setGeometry(QtCore.QRect(90, 40, 169, 113))
self.widget.setObjectName(_fromUtf8("widget"))
self.gridLayout = QtGui.QGridLayout(self.widget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.label_3 = QtGui.QLabel(self.widget)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.horizontalLayout.addWidget(self.label_3)
self.grade = QtGui.QLineEdit(self.widget)
self.grade.setObjectName(_fromUtf8("grade"))
self.horizontalLayout.addWidget(self.grade)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.label = QtGui.QLabel(self.widget)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout_2.addWidget(self.label)
self.sno = QtGui.QLineEdit(self.widget)
self.sno.setObjectName(_fromUtf8("sno"))
self.horizontalLayout_2.addWidget(self.sno)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtGui.QHBoxLayout()
self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
self.label_2 = QtGui.QLabel(self.widget)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayout_3.addWidget(self.label_2)
self.password = QtGui.QLineEdit(self.widget)
self.password.setEchoMode(QtGui.QLineEdit.Password)
self.password.setObjectName(_fromUtf8("password"))
self.horizontalLayout_3.addWidget(self.password)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
self.horizontalLayout_4 = QtGui.QHBoxLayout()
self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
spacerItem = QtGui.QSpacerItem(78, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem)
self.pushButton = QtGui.QPushButton(self.widget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout_4.addWidget(self.pushButton)
self.gridLayout.addLayout(self.horizontalLayout_4, 1, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Dialog.accept)
QtCore.QMetaObject.connectSlotsByName(Dialog)
Dialog.setTabOrder(self.grade, self.sno)
Dialog.setTabOrder(self.sno, self.password)
Dialog.setTabOrder(self.password, self.pushButton)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label_3.setText(_translate("Dialog", "年级", None))
self.label.setText(_translate("Dialog", "学号", None))
self.label_2.setText(_translate("Dialog", "密码", None))
self.pushButton.setText(_translate("Dialog", "确定", None))
F5运行不出错,至此已大功告成。那么在python中显示界面呢?新建python主程序如下:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
import test
class TestDialog(QDialog,test.Ui_Dialog):
def __init__(self,parent=None):
super(TestDialog,self).__init__(parent)
self.setupUi(self)
app=QApplication(sys.argv)
dialog=TestDialog()
dialog.show()
app.exec_()
1.其中import进来的模块test则是pyQT生成的py文件,下面一个test也是一样的。而Ui_Dialog则是生成的py文件的主类名称。2.dialog=TestDialog()为实例化一个对话框。通过dialog类下的show函数能够显示出界面。 3.通过类下Line Edit控件的text方法可以返回输入的数值。写法如
grade=dialog.grade.text();
sno=dialog.sno.text();
password=dialog.password.text().