| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import sys
- from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLineEdit, QFormLayout, QLabel, QPushButton, \
- QInputDialog
- class FormDialog(QDialog):
- def __init__(self):
- super().__init__()
- self.setWindowTitle("表单输入")
- # 创建表单布局
- self.layout = QFormLayout()
- # 创建输入框
- self.name_line_edit = QLineEdit(self)
- self.age_line_edit = QLineEdit(self)
- # 创建提交按钮
- # self.submit_button = QPushButton("提交", self)
- # self.submit_button.clicked.connect(self.on_submit)
- # 将控件添加到表单布局
- self.layout.addRow(QLabel("姓名:"), self.name_line_edit)
- self.layout.addRow(QLabel("年龄:"), self.age_line_edit)
- # self.layout.addWidget(self.submit_button)
- # 设置对话框的主布局
- self.setLayout(self.layout)
- def on_submit(self):
- # 获取输入的内容
- name = self.name_line_edit.text()
- age = self.age_line_edit.text()
- # 打印输入内容
- print(f"姓名: {name}")
- print(f"年龄: {age}")
- # 关闭对话框
- self.accept()
- class MainWindow(QDialog):
- def __init__(self):
- super().__init__()
- self.setWindowTitle("QInputDialog 表单")
- # 创建并显示表单对话框
- self.form_dialog = FormDialog()
- # 显示表单对话框
- self.form_dialog.exec_()
- # 创建并运行应用
- app = QApplication(sys.argv)
- window = MainWindow()
- window.show()
- sys.exit(app.exec_())
|