第一步:安装PyQt5库
在终端输入:
pip install pyqt5
第二步:复制下列代码
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFontclass InputWindow(QWidget):def __init__(self):super().__init__()self.name_input = QLineEdit()self.age_input = QLineEdit()self.weight_input = QLineEdit()self.heart_rate_input = QLineEdit()self.respiration_rate_input = QLineEdit()self.init_ui()def init_ui(self):self.setWindowTitle('输入用户数据')self.setGeometry(100, 100, 280, 170) # x, y, width, heightlayout = QVBoxLayout()# 设置整体字体font = QFont('Arial', 10)self.setFont(font)layout.addLayout(self.create_input("姓名:", self.name_input))layout.addLayout(self.create_input("年龄:", self.age_input))layout.addLayout(self.create_input("体重 (kg):", self.weight_input))layout.addLayout(self.create_input("心率 (bpm):", self.heart_rate_input))layout.addLayout(self.create_input("呼吸频率:", self.respiration_rate_input))submit_button = QPushButton("提交")submit_button.clicked.connect(self.submit_data)# 设置按钮样式submit_button.setFont(QFont('Arial', 10, QFont.Bold))submit_button.setStyleSheet("QPushButton { background-color: #4CAF50; color: white; }""QPushButton:pressed { background-color: #66BB6A; }")layout.addWidget(submit_button)self.setLayout(layout)# 设置窗口样式self.setStyleSheet("QWidget { background-color: #EEE; }""QLabel { color: #333; }""QLineEdit { background-color: #FFF; border: 1px solid #CCC; border-radius: 5px; padding: 5px; }")def create_input(self, label_text, line_edit):layout = QHBoxLayout()label = QLabel(label_text)label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)layout.addWidget(label)layout.addWidget(line_edit)return layoutdef submit_data(self):name = self.name_input.text()age = self.age_input.text()weight = self.weight_input.text()heart_rate = self.heart_rate_input.text()respiration_rate = self.respiration_rate_input.text()self.output_window = OutputWindow(name, age, weight, heart_rate, respiration_rate)self.output_window.show()class OutputWindow(QWidget):def __init__(self, name, age, weight, heart_rate, respiration_rate):super().__init__()self.name = nameself.age = ageself.weight = weightself.heart_rate = heart_rateself.respiration_rate = respiration_rateself.init_ui()def init_ui(self):self.setWindowTitle('显示用户数据')self.setGeometry(100, 100, 320, 200) # x, y, width, heightlayout = QVBoxLayout()font = QFont('Arial', 16, QFont.Bold)name_label = QLabel(f"姓名: {self.name}")age_label = QLabel(f"年龄: {self.age}")weight_label = QLabel(f"体重: {self.weight} kg")heart_rate_label = QLabel(f"心率: {self.heart_rate} bpm")respiration_rate_label = QLabel(f"呼吸频率: {self.respiration_rate}")name_label.setFont(font)age_label.setFont(font)weight_label.setFont(font)heart_rate_label.setFont(font)respiration_rate_label.setFont(font)layout.addWidget(name_label)layout.addWidget(age_label)layout.addWidget(weight_label)layout.addWidget(heart_rate_label)layout.addWidget(respiration_rate_label)self.setLayout(layout)self.setStyleSheet("QWidget { background-color: #FAFAFA; } QLabel { margin: 10px; }")if __name__ == '__main__':app = QApplication(sys.argv)ex = InputWindow()ex.show()sys.exit(app.exec_())
第三步:运行代码,输入信息,显示结果
运行代码后会弹出“用户数据输入”窗口:
输入数据后点击“提交”,接着会弹出“显示用户数据”界面: