麻烦整合成一个完整的代码可以吗
时间: 2024-10-20 12:14:04 浏览: 7
当然可以。这是一个完整的Python应用代码示例,包含了上述描述的功能:
```python
# 导入所需库
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QMessageBox, QVBoxLayout, QGridLayout
from PyQt5.QtCore import Qt
class RegistrationForm(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('用户注册')
self.setGeometry(100, 100, 300, 200)
# 创建布局
grid_layout = QGridLayout()
name_label = QLabel('姓名:')
self.name_input = QLineEdit()
grid_layout.addWidget(name_label, 0, 0)
grid_layout.addWidget(self.name_input, 0, 1)
gender_label = QLabel('性别:')
self.gender_input = QLineEdit()
grid_layout.addWidget(gender_label, 1, 0)
grid_layout.addWidget(self.gender_input, 1, 1)
hobby_label = QLabel('爱好:')
self.hobby_input = QLineEdit()
grid_layout.addWidget(hobby_label, 2, 0)
grid_layout.addWidget(self.hobby_input, 2, 1)
submit_button = QPushButton('提交', clicked=self.show_confirmation)
grid_layout.addWidget(submit_button, 3, 0, 1, 2)
self.setLayout(grid_layout)
def show_confirmation(self):
name = self.name_input.text()
gender = self.gender_input.text()
hobby = self.hobby_input.text()
if not all([name, gender, hobby]):
QMessageBox.warning(self, '错误', '请填写完整信息')
else:
QMessageBox.information(self, '确认', f'已记录:\n姓名:{name}\n性别:{gender}\n爱好:{hobby}')
if __name__ == "__main__":
app = QApplication(sys.argv)
form = RegistrationForm()
form.show()
sys.exit(app.exec_())
```
这个代码创建了一个简单的用户注册表单,包含了姓名、性别和爱好的输入框,以及一个提交按钮。当点击提交按钮时,会检查输入信息是否齐全,并在满足条件时显示确认消息。
阅读全文