Search code examples
pythonpyqtpyside6

How can I change the Main Window with a button and run the events from another window Pyside6


I want to change the Main Window through a button click, but when the main window change´s the event´s dont run.

The both window´s are already been created in qt Designer here is the code:

from PySide6.QtWidgets import *
from PySide6.QtWidgets import QPushButton
from PySide6.QtCore import  QObject
from PySide6.QtCore import  QCoreApplication
from PySide6.QtCore import *
from PySide6.QtGui import *

from PySide6 import QtWidgets, QtCore, QtGui
from ui_app import Ui_MainWindow
from ui_menu import Ui_MainWindow1
import sys

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("Login")

        self.pushButton.clicked.connect(self.trocarpag)

    def trocarpag(self):
        if self.textuser.toPlainText() == "" :
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Warning)
            msg.setText("Preencha o Username!!!")
            msg.exec()
        elif self.textpass.toPlainText() == "" :
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Warning)
            msg.setText("Preencha a Password!!!")
            msg.exec()
        else:
            ###Change the Main Window####`
             self.ui = Ui_MainWindow1()
             self.ui.setupUi(self)
            
if __name__ =="__main__":
    app = QApplication()
    window = MainWindow()
    window.show()
    app.exec()

Solution

  • A line of code self.ui = Ui_MainWindow1() creates just a member variable in your object window of class MainWindow. There are no code that would replace widgets in current window or show new window instead.