|
发表于 2024-3-9 20:42:03
|
显示全部楼层
好的,下面是使用Python PyQt6构建两个窗体的示例代码:
- import sys
- from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QProgressBar, QMessageBox, QMainWindow, QTextEdit, QComboBox
- from PyQt6.QtCore import Qt
- # 定义基础类
- class BaseWindow(QWidget):
- def __init__(self):
- super().__init__()
- self.setWindowTitle("基础窗体")
-
- # ESC事件处理
- self.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint)
-
- def keyPressEvent(self, event):
- if event.key() == Qt.Key.Key_Escape:
- self.close()
-
- def closeEvent(self, event):
- reply = QMessageBox.question(self, '确认退出', '是否确认退出程序?', QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
- if reply == QMessageBox.StandardButton.Yes:
- event.accept()
- else:
- event.ignore()
- # 定义A窗体
- class AWindow(BaseWindow):
- def __init__(self):
- super().__init__()
- self.setStyleSheet("background-color: lightblue;")
- self.resize(1440, 900)
-
- # 弹出对话框
- reply = QMessageBox.question(self, '欢迎使用', '是否开始使用本系统?', QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
- if reply == QMessageBox.StandardButton.No:
- self.close()
- return
-
- # 垂直布局(1)
- layout1 = QVBoxLayout()
- self.setLayout(layout1)
- # 创建标签
- label1 = QLabel("欢迎")
- label2 = QLabel("使用本系统")
- layout1.addWidget(label1)
- layout1.addWidget(label2)
- layout1.setAlignment(Qt.AlignmentFlag.AlignCenter)
- # 垂直布局(2)
- layout2 = QVBoxLayout()
- self.setLayout(layout2)
- # 创建用户名、密码框和登录按钮
- username_label = QLabel("用户名")
- password_label = QLabel("密码")
- username_input = QLineEdit()
- password_input = QLineEdit()
- login_button = QPushButton("登录")
- layout2.addWidget(username_label)
- layout2.addWidget(username_input)
- layout2.addWidget(password_label)
- layout2.addWidget(password_input)
- layout2.addWidget(login_button)
- layout2.setAlignment(Qt.AlignmentFlag.AlignCenter)
- # 垂直布局(3)
- layout3 = QVBoxLayout()
- self.setLayout(layout3)
- # 创建载入中标签和进度条
- loading_label = QLabel("载入中,请稍候…")
- progress_bar = QProgressBar()
- layout3.addWidget(loading_label)
- layout3.addWidget(progress_bar)
- # 垂直布局(4)
- layout4 = QVBoxLayout()
- self.setLayout(layout4)
- # 创建选项调整、帮助和退出按钮
- settings_button = QPushButton("选项调整")
- help_button = QPushButton("帮助")
- exit_button = QPushButton("退出")
- layout4.addWidget(settings_button)
- layout4.addWidget(help_button)
- layout4.addWidget(exit_button)
- layout4.setAlignment(Qt.AlignmentFlag.AlignBottom | Qt.AlignmentFlag.AlignRight)
- # 定义B窗体
- class BWindow(BaseWindow):
- def __init__(self):
- super().__init__()
- self.setStyleSheet("background-color: lightblue;")
- self.resize(1440, 900)
-
- # 垂直布局
- layout = QVBoxLayout()
- self.setLayout(layout)
- # 创建13个按钮和一个退出按钮
- for i in range(1, 14):
- button = QPushButton("按钮{}".format(i))
- layout.addWidget(button)
- exit_button = QPushButton("退出")
- layout.addWidget(exit_button)
- layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
- # 创建textEdit和comboBox
- text_edit1 = QTextEdit()
- text_edit2 = QTextEdit()
- text_edit3 = QTextEdit()
- combo_box = QComboBox()
- layout.addWidget(text_edit1)
- layout.addWidget(text_edit2)
- layout.addWidget(text_edit3)
- layout.addWidget(combo_box)
- layout.setAlignment(Qt.AlignmentFlag.AlignRight)
- if __name__ == "__main__":
- app = QApplication(sys.argv)
-
- a_window = AWindow()
- b_window = BWindow()
-
- a_window.show()
-
- sys.exit(app.exec())
复制代码
这是一个简单的示例,供参考。注意,由于PyQt6仍然是开发中的版本,可能存在一些不稳定或变动的地方。如果您遇到问题,请先查看官方文档或访问PyQt6的相关论坛寻求帮助。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|