这行代码运行的时候报错 AttributeError: 'RegisterWindow' object has no attribute 'closed'. Did you mean: 'close'?
RegisterWindow 继承于 QWidget
class RegisterWindow(QWidget):
就是现在如果我想实现子窗口打开的时候主窗口隐藏, 反之显示, 所以这里需要用一个子窗口的关闭事件, 但是出现这个问题如何解决?
# -*- coding: utf-8 -*-
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout
from registerWindow import RegisterWindow
from PyQt6 import QtCore, QtWidgets
class LoginWindow(QWidget):
def __init__(self):
super().__init__()
# 设置窗口大小
self.setGeometry(200, 200, 400, 200)
# 创建控件
self.username_label = QLabel('用户名')
self.username_input = QLineEdit()
self.password_label = QLabel('密码')
self.password_input = QLineEdit()
self.login_button = QPushButton('登录')
self.register_button = QPushButton('注册')
# 创建布局
layout = QVBoxLayout()
layout.addWidget(self.username_label)
layout.addWidget(self.username_input)
layout.addWidget(self.password_label)
layout.addWidget(self.password_input)
layout.addWidget(self.login_button)
layout.addWidget(self.register_button)
# 设置布局
self.setLayout(layout)
# 子窗口
self.registerWin = RegisterWindow()
self.registerWin.closed.connect(self.on_reg_window_closed)
# 连接信号和槽
self.login_button.clicked.connect(self.on_login_button_clicked)
self.register_button.clicked.connect(self.on_register_button_clicked)
def on_sub_window_closed(self):
# 子窗口已关闭
print("子窗口已关闭")
def on_login_button_clicked(self):
username = self.username_input.text()
password = self.password_input.text()
# 在这里可以添加验证用户输入的代码,如果验证通过则可以跳转到主界面
def on_register_button_clicked(self):
self.hide()
self.registerWin.show()
def on_reg_window_closed(self):
self.show()
if __name__ == '__main__':
app = QApplication([])
login_window = LoginWindow()
login_window.show()
app.exec()