PyQt5 – 创建一个用户表单来获取信息

PyQt5 – 创建一个用户表单来获取信息

在这篇文章中,我们将看到如何在PyQt5中创建一个用户表单。一个用户表单基本上是一个对话框,它使用户的数据输入更加可控,更容易被用户使用。有时在创建一个大型的用户界面应用程序时,需要创建用户表单,以获得基本信息。

实施步骤:

1.创建一个继承于QDialog的窗口类

2.添加窗口标题,并设置其几何形状

3.创建一个QGropBox对象

4.创建行编辑来获取姓名,旋转框来获取年龄,组合框来选择学位

5.创建一个createForm方法,该方法将创建一个表单

6.在createform方法中,创建一个新的表单。在创建表单的方法中,创建一个表单布局,并添加行

7.每一行都应该有一个标签和输入法,例如姓名标签和行编辑用于输入,类似的标签用于学位和年龄,组合框和旋转框用于输入。

8.创建一个QDialogButtonBox,用于确定和取消状态

9.为接受和拒绝的按钮添加动作

10.如果确定按钮被按下,就打印所有的信息,如果取消按钮被按下,窗口将被关闭。

下面是实现的过程

# 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()
 
    # create 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程