2017-09-25 70 views
1

在我的代碼中,我有2個小部件和1個主窗口。當我的程序開始時,主窗口的中心部件是「登錄」。我希望當我點擊註冊(登錄小工具的標籤),reg小部件打開。我這樣做,但後來我點擊登錄(註冊的小部件的標籤)相同的想法不發生。對不起英語不好。點擊標籤時如何打開小部件

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_())` 

回答

0

發生這種情況,因爲該方法setCentralWidget刪除參考當前插件(doc)。因此,當您嘗試再次顯示時,您的Python參考self.login指的是已刪除的內容。

有一種變通方法:

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

self.reg也許你會喜歡:分配None父當前中央部件只設置一個新的中央部件(這將迫使程序保持基準)前刪除表單並重新創建一個新的,如果用戶再次點擊按鈕註冊?

+0

是的。謝謝你的幫助。有效! –

+0

不錯!這個鏈接很有趣,如果你想在PyQt中更多地瞭解這類問題:[PyQt。如何在腳下拍攝自己](http://enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html) – PRMoureu

相關問題