清風 发表于 2024-3-9 20:41:29

新手求助!

本帖最后由 清風 于 2024-3-9 22:56 编辑

我重新考虑一下再来

FishC 发表于 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 小助理,如未能正确解答您的问题,请继续追问。

清風 发表于 2024-3-9 20:50:26

FishC 发表于 2024-3-9 20:42
好的,下面是使用Python PyQt6构建两个窗体的示例代码:




请加入用户名和密码录入界面及判断代码….

FishC 发表于 2024-3-9 20:51:10

清風 发表于 2024-03-09 20:50
请加入用户名和密码录入界面及判断代码….

好的,我已经更新了代码,加入了用户名和密码录入界面以及相关的判断代码。请看下面的代码:

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QProgressBar, QMessageBox
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)
      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("密码")
      self.username_input = QLineEdit()
      self.password_input = QLineEdit()
      self.login_button = QPushButton("登录")
      layout2.addWidget(username_label)
      layout2.addWidget(self.username_input)
      layout2.addWidget(password_label)
      layout2.addWidget(self.password_input)
      layout2.addWidget(self.login_button)
      layout2.setAlignment(Qt.AlignmentFlag.AlignCenter)

      # 绑定登录按钮的点击事件
      self.login_button.clicked.connect(self.login)

    def login(self):
      username = self.username_input.text()
      password = self.password_input.text()

      # 检查用户名和密码
      if username == "admin" and password == "123456":
            self.loading()
      else:
            QMessageBox.warning(self, "登录失败", "用户名或密码错误!")

    def loading(self):
      # 垂直布局(3)
      layout3 = QVBoxLayout()
      self.setLayout(layout3)

      # 创建载入中标签和进度条
      loading_label = QLabel("载入中,请稍候…")
      self.progress_bar = QProgressBar()
      layout3.addWidget(loading_label)
      layout3.addWidget(self.progress_bar)

      # 设置进度条属性
      self.progress_bar.setMinimum(0)
      self.progress_bar.setMaximum(100)
      self.progress_bar.setValue(0)

      # 模拟加载过程
      for i in range(101):
            self.progress_bar.setValue(i)
            QApplication.processEvents()# 刷新界面,防止界面卡住
            if i == 100:
                self.close()
                b_window.show()

# 定义B窗体
class BWindow(BaseWindow):
    def __init__(self):
      super().__init__()
      self.setStyleSheet("background-color: lightblue;")
      self.resize(1440, 900)
      
      # 垂直布局
      layout = QVBoxLayout(self)
      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)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    a_window = AWindow()
    b_window = BWindow()

    a_window.show()

    sys.exit(app.exec())

在A窗体中,我们添加了用户名和密码的输入框以及登录按钮。当用户点击登录按钮时,会检查用户名和密码是否正确。如果正确,会调用`loading()`方法来显示进度条。在`loading()`方法中,我们使用for循环模拟加载过程,并通过`setValue()`方法更新进度条的值。当进度条达到100时,关闭A窗体并打开B窗体。

请注意,此代码仅供参考,并可能需要根据您的实际需求进行修改和调整。如果遇到任何问题,请随时向我提问!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

sfqxx 发表于 2024-3-9 21:44:02

不要设置权限!
页: [1]
查看完整版本: 新手求助!