单击标签时如何打开小部件

时间:2017-09-25 21:23:13

标签: python

在我的代码中,我有2个小部件和1个主窗口。当我的程序启动时,主窗口的中央窗口小部件是“登录”。我希望当我点击注册(登录小部件的标签)时,注册小部件打开。我这样做,但是然后我点击登录(注册的小部件的标签)同样认为不发生。抱歉英文不好。

import sys
import os
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QMainWindow
from PyQt5 import QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class main(QMainWindow):

    def __init__(self):

        super().__init__()
        self.login = login()
        self.reg = reg()
        self.setCentralWidget(self.login)
        self.setWindowTitle("Main")
        self.init_ui()

    def init_ui(self):

        self.login.label_4.mousePressEvent = self.click
        self.show()

    def click(self,event):

        if event:
            self.setCentralWidget(self.reg)
            self.reg.label_4.mousePressEvent = self.click_2

    def click_2(self,event):

        if event:
            self.setCentralWidget(self.login)
            self.login.label_4.mousePressEvent = self.click
class login(QWidget):

    def __init__(self):

        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.label_1 = QLabel("Login:       ")
        self.line_1 = QLineEdit()
        self.h_box = QHBoxLayout()
        self.h_box.addWidget(self.label_1)
        self.h_box.addWidget(self.line_1)

        self.label_2 = QLabel("Password:")
        self.line_2 = QLineEdit()
        self.line_2.setEchoMode(QLineEdit.Password)
        self.h_box2 = QHBoxLayout()
        self.h_box2.addWidget(self.label_2)
        self.h_box2.addWidget(self.line_2)

        self.buton_1 = QPushButton("Login")

        self.label_4 = QLabel("Register")
        self.label_4.setStyleSheet("color : blue")
        self.label_4.setAlignment(Qt.AlignCenter)

        self.v_box = QVBoxLayout()
        self.v_box.addLayout(self.h_box)
        self.v_box.addLayout(self.h_box2)
        self.v_box.addWidget(self.buton_1)
        self.v_box.addWidget(self.label_4)

        self.setLayout(self.v_box)
class reg(QWidget):

    def __init__(self):

        super().__init__()
        self.init_ui()

    def init_ui(self):

        self.label_1 = QLabel("Login:                    ")
        self.line_1 = QLineEdit()
        self.h_box1 = QHBoxLayout()
        self.h_box1.addWidget(self.label_1)
        self.h_box1.addWidget(self.line_1)

        self.label_2 = QLabel("Password              ")
        self.line_2 = QLineEdit()
        self.line_2.setEchoMode(QLineEdit.Password)
        self.line_2.resize(300,300)
        self.h_box2 = QHBoxLayout()
        self.h_box2.addWidget(self.label_2)
        self.h_box2.addWidget(self.line_2)

        self.label_3 = QLabel("Retype password:")
        self.line_3 = QLineEdit()
        self.line_3.setEchoMode(QLineEdit.Password)
        self.h_box3 = QHBoxLayout()
        self.h_box3.addWidget(self.label_3)
        self.h_box3.addWidget(self.line_3)

        self.buton_1 = QPushButton("Register")
        self.label_4 = QLabel("Login")
        self.label_4.setAlignment(Qt.AlignCenter)
        self.label_4.setStyleSheet('color: blue')

        self.v_box = QVBoxLayout()
        self.v_box.addLayout(self.h_box1)
        self.v_box.addLayout(self.h_box2)
        self.v_box.addLayout(self.h_box3)
        self.v_box.addWidget(self.buton_1)
        self.v_box.addWidget(self.label_4)
        self.setLayout(self.v_box)

        self.setWindowTitle("Login")


app = QApplication(sys.argv)
main_window = main()
sys.exit(app.exec_())`

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为方法setCentralWidget删除了对当前小部件(doc)的引用。因此,当您尝试再次显示它时,您的Python引用self.login指的是已删除的内容。

有一种解决方法:在设置新的中央窗口小部件之前为当前中央窗口小部件分配None父窗口(这将强制程序保留引用):

self.centralWidget().setParent(None)
self.setCentralWidget(self.reg)

对于self.reg,如果用户再次点击按钮注册,您可能会更喜欢删除该表单并重新创建一个新表单?