鱼C论坛

 找回密码
 立即注册
楼主: 凊风

PySide6求教~

  [复制链接]
 楼主| 发表于 2024-4-8 10:31:33 | 显示全部楼层
FishC 发表于 2024-4-8 10:30
非常抱歉,上述代码确实没有正确连接退出按钮的点击事件。以下是修复后的代码:

button_exit点击也要提示确认的,不能直接退出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-8 10:31:50 | 显示全部楼层
凊风 发表于 2024-04-08 10:31
button_exit点击也要提示确认的,不能直接退出


非常抱歉,我理解错了您的意思。要实现点击退出按钮时弹出确认对话框,请修改`setup_exit_button`方法中的代码如下:

  1. from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, QMessageBox

  2. class BaseWindow(QMainWindow):
  3.     def __init__(self):
  4.         super().__init__()
  5.         self.setWindowTitle("Base Window")
  6.         self.setGeometry(100, 100, 800, 600)
  7.         palette = self.palette()
  8.         palette.setColor(QPalette.Window, QColor("#87CEFA"))  # 淡蓝色背景
  9.         self.setPalette(palette)
  10.         self.setup_exit_button()

  11.     def setup_exit_button(self):
  12.         button_exit = QPushButton("Exit", self)
  13.         button_exit.clicked.connect(self.confirm_exit)
  14.         button_exit.setStyleSheet("QPushButton {color: red;}")
  15.         button_exit.move(self.width() - 100, 10)
  16.         button_exit.setParent(self)

  17.     def confirm_exit(self):
  18.         reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  19.         if reply == QMessageBox.Yes:
  20.             self.close()
复制代码


现在,点击退出按钮会弹出一个确认对话框,询问用户是否真的要退出系统。如果用户点击了"是",系统将退出;否则,对话框会消失,用户可以继续操作应用程序。

非常抱歉给您带来困惑,现在退出按钮应该能正常工作了。再次感谢您的耐心!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-8 10:33:11 | 显示全部楼层
FishC 发表于 2024-4-8 10:31
非常抱歉,我理解错了您的意思。要实现点击退出按钮时弹出确认对话框,请修改`setup_exit_button`方法 ...

button_exit点不动,好像不是个按钮一样。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-8 10:33:24 | 显示全部楼层
凊风 发表于 2024-04-08 10:33
button_exit点不动,好像不是个按钮一样。


非常抱歉给您带来困扰,请确认以下几点:

1. 您确保按钮已经正确地创建并添加到窗口中;
2. 您在使用PySide6时导入了正确的模块。

请参考以下代码示例,其中包含了创建一个点击退出按钮的完整示例:

  1. from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox

  2. class MainWindow(QMainWindow):
  3.     def __init__(self):
  4.         super().__init__()
  5.         self.setWindowTitle("Exit Button Example")
  6.         self.setGeometry(100, 100, 300, 200)
  7.         
  8.         self.setup_exit_button()
  9.    
  10.     def setup_exit_button(self):
  11.         button_exit = QPushButton("Exit", self)
  12.         button_exit.clicked.connect(self.confirm_exit)
  13.         button_exit.move(100, 100)
  14.    
  15.     def confirm_exit(self):
  16.         reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  17.         if reply == QMessageBox.Yes:
  18.             self.close()

  19. if __name__ == "__main__":
  20.     app = QApplication([])
  21.     window = MainWindow()
  22.     window.show()
  23.     app.exec()
复制代码


如果您仍然遇到问题,建议您检查代码中是否存在其他可能影响按钮可用性的因素,并确保所有必要的依赖库已正确安装。如果问题仍然存在,请提供更详细的代码和错误信息,以便能够更好地帮助您解决问题。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-8 10:36:33 | 显示全部楼层
FishC 发表于 2024-4-8 10:31
非常抱歉,我理解错了您的意思。要实现点击退出按钮时弹出确认对话框,请修改`setup_exit_button`方法 ...

button_exit点不动,好像不是个按钮一样。

但button_login可以正常点得动。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-8 10:36:48 | 显示全部楼层
凊风 发表于 2024-04-08 10:36
button_exit点不动,好像不是个按钮一样。

但butto ...


非常抱歉,我之前的回答有误。你可以尝试为退出按钮设置一个布局,并将按钮放入布局中。代码如下:from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, QMessageBoxclass BaseWindow(QMainWindow):    def __init__(self):        super().__init__()        self.setWindowTitle("Base Window")        self.setGeometry(100, 100, 800, 600)        palette = self.palette()        palette.setColor(QPalette.Window, QColor("#87CEFA"))  # 淡蓝色背景        self.setPalette(palette)        self.setup_ui()    def setup_ui(self):        main_widget = QWidget(self)        layout = QVBoxLayout(main_widget)        label = QLabel("Username:", main_widget)        layout.addWidget(label)        line_edit = QLineEdit(main_widget)        layout.addWidget(line_edit)        button_login = QPushButton("Login", main_widget)        layout.addWidget(button_login)        button_exit = QPushButton("Exit", main_widget)        layout.addWidget(button_exit)        button_exit.clicked.connect(self.confirm_exit)        self.setCentralWidget(main_widget)    def confirm_exit(self):        reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)        if reply == QMessageBox.Yes:            self.close()app = QApplication([])window = BaseWindow()window.show()app.exec()这样,退出按钮应该能够正常显示并且可以点击了。再次对之前的错误回答表示歉意,希望这次能解决您的问题。感谢您的耐心!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-8 10:37:06 | 显示全部楼层
FishC 发表于 2024-4-8 10:36
非常抱歉,我之前的回答有误。你可以尝试为退出按钮设置一个布局,并将按钮放入布局中。代码如下:from ...

回复请注意Python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-8 10:37:17 | 显示全部楼层
凊风 发表于 2024-04-08 10:37
回复请注意Python格式


非常抱歉,我之前的回答没有按照Python的格式来进行展示。以下是修正后的代码:

  1. from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, QMessageBox

  2. class BaseWindow(QMainWindow):
  3.     def __init__(self):
  4.         super().__init__()
  5.         self.setWindowTitle("Base Window")
  6.         self.setGeometry(100, 100, 800, 600)
  7.         palette = self.palette()
  8.         palette.setColor(QPalette.Window, QColor("#87CEFA"))  # 淡蓝色背景
  9.         self.setPalette(palette)
  10.         self.setup_ui()

  11.     def setup_ui(self):
  12.         main_widget = QWidget(self)
  13.         layout = QVBoxLayout(main_widget)

  14.         label = QLabel("Username:", main_widget)
  15.         layout.addWidget(label)

  16.         line_edit = QLineEdit(main_widget)
  17.         layout.addWidget(line_edit)

  18.         button_login = QPushButton("Login", main_widget)
  19.         layout.addWidget(button_login)

  20.         button_exit = QPushButton("Exit", main_widget)
  21.         layout.addWidget(button_exit)
  22.         button_exit.clicked.connect(self.confirm_exit)

  23.         self.setCentralWidget(main_widget)

  24.     def confirm_exit(self):
  25.         reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  26.         if reply == QMessageBox.Yes:
  27.             self.close()

  28. app = QApplication()
  29. window = BaseWindow()
  30. window.show()
  31. app.exec()
复制代码


再次对之前的错误回答表示歉意,希望这次能解决您的问题。感谢您的耐心!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-8 10:53:07 | 显示全部楼层
FishC 发表于 2024-4-8 10:37
非常抱歉,我之前的回答没有按照Python的格式来进行展示。以下是修正后的代码:

请问这一段代码:

import sys
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QPalette, QColor, QFont
from PySide6.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QLabel,
                               QLineEdit, QPushButton, QWidget, QMessageBox)


class BaseWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Base Window")
        self.setGeometry(100, 100, 800, 600)
        palette = self.palette()
        palette.setColor(QPalette.Window, QColor("#87CEFA"))  # 淡蓝色背景
        self.setPalette(palette)
        self.setup_ui()

    def setup_ui(self):
        main_widget = QWidget(self)
        layout = QVBoxLayout(main_widget)

        label = QLabel("Username:", main_widget)
        layout.addWidget(label)

        line_edit = QLineEdit(main_widget)
        layout.addWidget(line_edit)

        button_login = QPushButton("Login", main_widget)
        layout.addWidget(button_login)

        button_exit = QPushButton("Exit", main_widget)
        layout.addWidget(button_exit)
        button_exit.clicked.connect(self.confirm_exit)

        self.setCentralWidget(main_widget)

    def confirm_exit(self):
        reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            self.close()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Escape:  # 捕捉Esc键事件
            reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No,
                                         QMessageBox.No)
            if reply == QMessageBox.Yes:
                self.close()


class WelcomeWindow(BaseWindow):
    switch_to_login = Signal()  # 切换到登录界面信号

    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        label = QLabel("Welcome to the App!", self)
        label.setFont(QFont("Arial", 24, QFont.Bold))
        layout.addWidget(label)
        button_login = QPushButton("Login", self)
        button_login.clicked.connect(self.switch_to_login.emit)
        layout.addWidget(button_login)
        central_widget = QWidget(self)
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)


class LoginWindow(BaseWindow):
    login_success = Signal()  # 登录成功信号

    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        label_username = QLabel("Username:", self)
        layout.addWidget(label_username)
        self.input_username = QLineEdit(self)
        layout.addWidget(self.input_username)
        label_password = QLabel("Password:", self)
        layout.addWidget(label_password)
        self.input_password = QLineEdit(self)
        self.input_password.setEchoMode(QLineEdit.Password)
        layout.addWidget(self.input_password)
        button_login = QPushButton("Login", self)
        button_login.clicked.connect(self.login)
        layout.addWidget(button_login)
        central_widget = QWidget(self)
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

    def login(self):
        username = self.input_username.text()
        password = self.input_password.text()
        # 进行用户名和密码的验证逻辑,这里只做简单演示
        if username == "1" and password == "1":
            self.login_success.emit()
        else:
            QMessageBox.warning(self, "登录失败", "用户名或密码错误,请重新输入。")
            self.input_username.clear()
            self.input_password.clear()


class MainWindow(BaseWindow):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        button1 = QPushButton("Button 1", self)
        layout.addWidget(button1)
        button2 = QPushButton("Button 2", self)
        layout.addWidget(button2)
        button3 = QPushButton("Button 3", self)
        layout.addWidget(button3)
        button4 = QPushButton("Button 4", self)
        layout.addWidget(button4)
        button5 = QPushButton("Button 5", self)
        layout.addWidget(button5)
        central_widget = QWidget(self)
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    welcome_window = WelcomeWindow()
    login_window = LoginWindow()
    main_window = MainWindow()
    login_window.login_success.connect(main_window.show)
    welcome_window.switch_to_login.connect(login_window.show)
    login_window.show()
    sys.exit(app.exec())

button_exit为什么会点不动呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-8 10:53:32 | 显示全部楼层
凊风 发表于 2024-04-08 10:53
请问这一段代码:

import sys
from PySide6.QtCor ...


根据您提供的代码,`button_exit` 是 `BaseWindow` 类的一部分,并在 `setup_ui` 方法中创建。根据我的理解,您想要在点击 `button_exit` 按钮时触发 `confirm_exit` 方法来关闭窗口。然而,由于 `button_exit` 是在 `BaseWindow` 类中定义的局部变量,无法在外部访问。

为了解决这个问题,您可以将 `button_exit` 定义为 `BaseWindow` 类的成员变量(属性),以便在其他方法中也能访问到它。以下是修改后的代码示例:

  1. from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, QMessageBox

  2. class BaseWindow(QMainWindow):
  3.     def __init__(self):
  4.         super().__init__()
  5.         self.setWindowTitle("Base Window")
  6.         self.setGeometry(100, 100, 800, 600)
  7.         palette = self.palette()
  8.         palette.setColor(QPalette.Window, QColor("#87CEFA"))  # 淡蓝色背景
  9.         self.setPalette(palette)
  10.         self.setup_ui()

  11.     def setup_ui(self):
  12.         main_widget = QWidget(self)
  13.         layout = QVBoxLayout(main_widget)
  14.         label = QLabel("Username:", main_widget)
  15.         layout.addWidget(label)
  16.         line_edit = QLineEdit(main_widget)
  17.         layout.addWidget(line_edit)
  18.         button_login = QPushButton("Login", main_widget)
  19.         layout.addWidget(button_login)
  20.         self.button_exit = QPushButton("Exit", main_widget)  # 修改这里,将 button_exit 设为成员变量
  21.         layout.addWidget(self.button_exit)
  22.         self.button_exit.clicked.connect(self.confirm_exit)
  23.         self.setCentralWidget(main_widget)

  24.     def confirm_exit(self):
  25.         reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  26.         if reply == QMessageBox.Yes:
  27.             self.close()

  28.     def keyPressEvent(self, event):
  29.         if event.key() == Qt.Key_Escape:  # 捕捉Esc键事件
  30.             reply = QMessageBox.question(self, "确认", "确定要退出系统吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  31.             if reply == QMessageBox.Yes:
  32.                 self.close()

  33. class WelcomeWindow(BaseWindow):
  34.     # ...

  35. class LoginWindow(BaseWindow):
  36.     # ...

  37. if __name__ == "__main__":
  38.     app = QApplication([])
  39.     welcome_window = WelcomeWindow()
  40.     login_window = LoginWindow()
  41.     welcome_window.switch_to_login.connect(login_window.show)
  42.     login_window.show()
  43.     app.exec()
复制代码


通过将 `button_exit` 定义为 `BaseWindow` 类的成员变量,在 `confirm_exit` 方法中就可以正常触发关闭窗口的逻辑了。希望这次回答能够帮助到您!如果还有疑问,请随时询问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-8 11:00:18 | 显示全部楼层
FishC 发表于 2024-4-8 10:53
根据您提供的代码,`button_exit` 是 `BaseWindow` 类的一部分,并在 `setup_ui` 方法中创建。根据我的 ...

这样改后,没有看到self.button_exit,

我希望self.button_exit放在BaseWindow右下角,以后的所有窗体都可以继承。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-8 11:01:14 | 显示全部楼层
凊风 发表于 2024-04-08 11:00
这样改后,没有看到self.button_exit,

我希望self ...


非常抱歉上一次的回答没有解决您的问题。我明白您的需求是在`BaseWindow`的右下角显示`button_exit`并使其可点击。为了实现这一目标,您可以使用布局管理器来放置`button_exit`按钮,并将其添加到主窗口的底部水平布局中。以下是修改后的代码示例:

  1. from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, QMessageBox

  2. class BaseWindow(QMainWindow):
  3.     def __init__(self):
  4.         super().__init__()
  5.         self.setWindowTitle("Base Window")
  6.         self.setGeometry(100, 100, 800, 600)
  7.         palette = self.palette()
  8.         palette.setColor(self.backgroundRole(), QColor("#87CEFA"))  # 淡蓝色背景
  9.         self.setPalette(palette)
  10.         self.setup_ui()

  11.     def setup_ui(self):
  12.         main_widget = QWidget(self)
  13.         layout = QVBoxLayout(main_widget)

  14.         label = QLabel("Username:", main_widget)
  15.         layout.addWidget(label)

  16.         line_edit = QLineEdit(main_widget)
  17.         layout.addWidget(line_edit)

  18.         button_login = QPushButton("Login", main_widget)
  19.         layout.addWidget(button_login)

  20.         # 创建水平布局并将 button_exit 添加到其中
  21.         hbox = QHBoxLayout()
  22.         layout.addLayout(hbox)

  23.         self.button_exit = QPushButton("Exit", main_widget)
  24.         hbox.addStretch(1)  # 右对齐
  25.         hbox.addWidget(self.button_exit)

  26.         button_login.clicked.connect(self.login)
  27.         self.button_exit.clicked.connect(self.confirm_exit)

  28.         self.setCentralWidget(main_widget)

  29.     def confirm_exit(self):
  30.         reply = QMessageBox.question(
  31.             self, "确认", "确定要退出系统吗?",
  32.             QMessageBox.Yes | QMessageBox.No, QMessageBox.No
  33.         )
  34.         if reply == QMessageBox.Yes:
  35.             self.close()

  36.     def login(self):
  37.         pass

  38.     def keyPressEvent(self, event):
  39.         if event.key() == Qt.Key_Escape:
  40.             reply = QMessageBox.question(
  41.                 self, "确认", "确定要退出系统吗?",
  42.                 QMessageBox.Yes | QMessageBox.No, QMessageBox.No
  43.             )
  44.             if reply == QMessageBox.Yes:
  45.                 self.close()

  46. class WelcomeWindow(BaseWindow):
  47.     pass

  48. class LoginWindow(BaseWindow):
  49.     pass

  50. if __name__ == "__main__":
  51.     app = QApplication()

  52.     welcome_window = WelcomeWindow()
  53.     login_window = LoginWindow()

  54.     welcome_window.show()

  55.     app.exec()
复制代码


在上面的代码中,我通过创建一个水平布局 `hbox` 并使用 `addStretch` 方法将`button_exit`按钮右对齐,随意空出一些边距。然后将`button_exit`添加到水平布局中。这样,`button_exit`就会显示在`BaseWindow`的右下角。

希望这次回答能够解决您的问题。如有任何疑问,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-8 11:09:15 | 显示全部楼层
FishC 发表于 2024-4-8 11:01
非常抱歉上一次的回答没有解决您的问题。我明白您的需求是在`BaseWindow`的右下角显示`button_exit`并 ...

  line 39, in setup_ui
    self.button_login.clicked.connect(self.login)
                                      ^^^^^^^^^^
AttributeError: 'WelcomeWindow' object has no attribute 'login'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-8 11:09:44 | 显示全部楼层
凊风 发表于 2024-04-08 11:09
line 39, in setup_ui
    self.button_login.clicke ...


非常抱歉,我在示例代码中犯了一个错误。`WelcomeWindow`和`LoginWindow`类是继承自`BaseWindow`的,但它们没有定义`login`方法。如果您希望在子类中实现不同的功能,您可以在子类中重写`login`方法。以下是修正后的示例代码:from PySide6.QtCore import Qtfrom PySide6.QtGui import QColorfrom PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, QMessageBoxclass BaseWindow(QMainWindow):    def __init__(self):        super().__init__()        self.setWindowTitle("Base Window")        self.setGeometry(100, 100, 800, 600)        palette = self.palette()        palette.setColor(self.backgroundRole(), QColor("#87CEFA"))  # 淡蓝色背景        self.setPalette(palette)        self.setup_ui()    def setup_ui(self):        main_widget = QWidget(self)        layout = QVBoxLayout(main_widget)        label = QLabel("Username:", main_widget)        layout.addWidget(label)        line_edit = QLineEdit(main_widget)        layout.addWidget(line_edit)        button_login = QPushButton("Login", main_widget)        layout.addWidget(button_login)        hbox = QHBoxLayout()        layout.addLayout(hbox)        self.button_exit = QPushButton("Exit", main_widget)        hbox.addStretch(1)  # 右对齐        hbox.addWidget(self.button_exit)        button_login.clicked.connect(self.login)        self.button_exit.clicked.connect(self.confirm_exit)        self.setCentralWidget(main_widget)    def confirm_exit(self):        reply = QMessageBox.question(            self, "确认", "确定要退出系统吗?",            QMessageBox.Yes | QMessageBox.No, QMessageBox.No        )        if reply == QMessageBox.Yes:            self.close()    def login(self):        pass    def keyPressEvent(self, event):        if event.key() == Qt.Key_Escape:            reply = QMessageBox.question(                self, "确认", "确定要退出系统吗?",                QMessageBox.Yes | QMessageBox.No, QMessageBox.No            )            if reply == QMessageBox.Yes:                self.close()class WelcomeWindow(BaseWindow):    def login(self):        print("WelcomeWindow login")class LoginWindow(BaseWindow):    def login(self):        print("LoginWindow login")if __name__ == "__main__":    app = QApplication()    welcome_window = WelcomeWindow()    login_window = LoginWindow()    welcome_window.show()    app.exec()请注意,我在`WelcomeWindow`和`LoginWindow`中分别重写了`login`方法,并在每个子类中打印了不同的消息。您可以根据需要在这些方法中添加您自己的逻辑。希望这次的答案能够帮助到您,如有任何问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-8 11:14:32 | 显示全部楼层
FishC 发表于 2024-4-8 11:09
非常抱歉,我在示例代码中犯了一个错误。`WelcomeWindow`和`LoginWindow`类是继承自`BaseWindow`的,但 ...

回复请注意Python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-8 11:14:59 | 显示全部楼层
凊风 发表于 2024-04-08 11:14
回复请注意Python格式


非常抱歉,我注意到我的回答中的代码格式化不正确。以下是修正后的示例代码:
  1. from PySide6.QtCore import Qt
  2. from PySide6.QtGui import QColor
  3. from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QWidget, QMessageBox

  4. class BaseWindow(QMainWindow):
  5.     def __init__(self):
  6.         super().__init__()
  7.         self.setWindowTitle("Base Window")
  8.         self.setGeometry(100, 100, 800, 600)
  9.         palette = self.palette()
  10.         palette.setColor(self.backgroundRole(), QColor("#87CEFA"))  # 淡蓝色背景
  11.         self.setPalette(palette)
  12.         self.setup_ui()

  13.     def setup_ui(self):
  14.         main_widget = QWidget(self)
  15.         layout = QVBoxLayout(main_widget)

  16.         label = QLabel("Username:", main_widget)
  17.         layout.addWidget(label)

  18.         line_edit = QLineEdit(main_widget)
  19.         layout.addWidget(line_edit)

  20.         button_login = QPushButton("Login", main_widget)
  21.         layout.addWidget(button_login)

  22.         hbox = QHBoxLayout()
  23.         layout.addLayout(hbox)

  24.         self.button_exit = QPushButton("Exit", main_widget)
  25.         hbox.addStretch(1)  # 右对齐
  26.         hbox.addWidget(self.button_exit)

  27.         button_login.clicked.connect(self.login)
  28.         self.button_exit.clicked.connect(self.confirm_exit)

  29.         self.setCentralWidget(main_widget)

  30.     def confirm_exit(self):
  31.         reply = QMessageBox.question(
  32.             self, "确认", "确定要退出系统吗?",
  33.             QMessageBox.Yes | QMessageBox.No, QMessageBox.No
  34.         )
  35.         if reply == QMessageBox.Yes:
  36.             self.close()

  37.     def login(self):
  38.         pass

  39.     def keyPressEvent(self, event):
  40.         if event.key() == Qt.Key_Escape:
  41.             reply = QMessageBox.question(
  42.                 self, "确认", "确定要退出系统吗?",
  43.                 QMessageBox.Yes | QMessageBox.No, QMessageBox.No
  44.             )
  45.             if reply == QMessageBox.Yes:
  46.                 self.close()

  47. class WelcomeWindow(BaseWindow):
  48.     def login(self):
  49.         print("WelcomeWindow login")

  50. class LoginWindow(BaseWindow):
  51.     def login(self):
  52.         print("LoginWindow login")

  53. if __name__ == "__main__":
  54.     app = QApplication()
  55.     welcome_window = WelcomeWindow()
  56.     login_window = LoginWindow()
  57.     welcome_window.show()
  58.     app.exec()
复制代码

非常抱歉给您带来了困扰,请检查上述代码是否满足您的需求,如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 01:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表