PyQt5 – 创建用户表单以获取信息
在本文中,我们将了解如何在 PyQt5 中创建用户表单。用户表单基本上是一个对话框,它使用户数据输入更可控,更易于用户使用。有时在创建大型用户界面应用程序时,需要创建用户表单以获取基本信息。
Implementation steps :
1. Create a window class that inherits QDialog
2. Add window title and set its geometry
3. Create a QGropBox object
4. Create line edit to get the name, spin box to get the age and combo box to select the degree
5. Create a createForm method that will create a form
6. Inside the create form method, create a form layout and add rows
7. Each row should be has a label and the input method example name label and line edit for input, similarly label for degree and age and combo box and spin box for input.
8. Create a QDialogButtonBox for ok and cancel state
9. Add action to the button for acceptance and for rejection
10. If ok button is pressed just print all the information and if the cancel button is pressed window will get closed
下面是实现
Python3
# importing libraries
from PyQt5.QtWidgets import *
import sys
# creating a class
# that inherits the QDialog class
class Window(QDialog):
# constructor
def __init__(self):
super(Window, self).__init__()
# setting window title
self.setWindowTitle("Python")
# setting geometry to the window
self.setGeometry(100, 100, 300, 400)
# creating a group box
self.formGroupBox = QGroupBox("Form 1")
# creating spin box to select age
self.ageSpinBar = QSpinBox()
# creating combo box to select degree
self.degreeComboBox = QComboBox()
# adding items to the combo box
self.degreeComboBox.addItems(["BTech", "MTech", "PhD"])
# creating a line edit
self.nameLineEdit = QLineEdit()
# calling the method that create the form
self.createForm()
# creating a dialog button for ok and cancel
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
# adding action when form is accepted
self.buttonBox.accepted.connect(self.getInfo)
# adding action when form is rejected
self.buttonBox.rejected.connect(self.reject)
# creating a vertical layout
mainLayout = QVBoxLayout()
# adding form group box to the layout
mainLayout.addWidget(self.formGroupBox)
# adding button box to the layout
mainLayout.addWidget(self.buttonBox)
# setting lay out
self.setLayout(mainLayout)
# get info method called when form is accepted
def getInfo(self):
# printing the form information
print("Person Name : {0}".format(self.nameLineEdit.text()))
print("Degree : {0}".format(self.degreeComboBox.currentText()))
print("Age : {0}".format(self.ageSpinBar.text()))
# closing the window
self.close()
# creat form method
def createForm(self):
# creating a form layout
layout = QFormLayout()
# adding rows
# for name and adding input text
layout.addRow(QLabel("Name"), self.nameLineEdit)
# for degree and adding combo box
layout.addRow(QLabel("Degree"), self.degreeComboBox)
# for age and adding spin box
layout.addRow(QLabel("Age"), self.ageSpinBar)
# setting layout
self.formGroupBox.setLayout(layout)
# main method
if __name__ == '__main__':
# create pyqt5 app
app = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# showing the window
window.show()
# start the app
sys.exit(app.exec())
输出 :
Person Name : Geek
Degree : BTech
Age : 20