How to change the contents of a window in PyQT 5?

0
I'm using PyQT 5 to develop a user interface and I'm having a question: how do I change the contents of an area within the main window? I want to do this because I do not want to have to create a new window every time I want a different layout. This way, when the user clicks a menu button the contents of the window changes according to the button. Currently I'm doing this: I have two frames, one pro content and another pro menu. Every time a button inside the menu is clicked, a function is called and removes all the current elements of the frame, and then creates the new elements.

Currently, my code looks something like this:

from PyQt5 import QtCore, QtWidgets
import sip

class Interface(object):
    def __init__(self, MainWindow):

        # Default Code
        MainWindow.setObjectName("engsoft")
        MainWindow.resize(761, 433)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        MainWindow.setCentralWidget(self.centralWidget)

        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)

        # Define Side Menu Frame
        self.sidemenu_frame = QtWidgets.QFrame(self.centralWidget)
        self.sidemenu_frame.setGeometry(QtCore.QRect(0, 0, 161, 411))
        self.sidemenu_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.sidemenu_frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.sidemenu_frame.setObjectName("sidemenu_frame")

        # Initialize Main Frame with nothing
        self.main_frame = QtWidgets.QFrame(self.centralWidget)
        self.main_frame.setEnabled(True)
        self.main_frame.setGeometry(QtCore.QRect(160, 0, 601, 411))
        self.main_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.main_frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.main_frame.setObjectName("main_frame")

        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "engsoft"))
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.currentFrame = ""

    def hide_current_frame(self):
        if self.currentFrame == "":
            pass

        elif self.currentFrame == "login":
            self.remove_login()

        self.main_frame.setVisible(False)

    def new_frame(self, current_frame):
        self.hide_current_frame()
        self.currentFrame = current_frame

    def create_login(self):
        self.new_frame("login")

        self.label_login = QtWidgets.QLabel(self.main_frame)
        self.label_login.setGeometry(QtCore.QRect(70, 150, 41, 21))
        self.label_login.setObjectName("label_login")
        self.label_login.setText("Login:")

        self.label_senha = QtWidgets.QLabel(self.main_frame)
        self.label_senha.setGeometry(QtCore.QRect(70, 180, 41, 21))
        self.label_senha.setObjectName("label_senha")
        self.label_senha.setText("Senha:")

        self.input_login = QtWidgets.QLineEdit(self.main_frame)
        self.input_login.setGeometry(QtCore.QRect(120, 150, 113, 22))
        self.input_login.setObjectName("input_login")

        self.input_senha = QtWidgets.QLineEdit(self.main_frame)
        self.input_senha.setGeometry(QtCore.QRect(120, 180, 113, 22))
        self.input_senha.setEchoMode(QtWidgets.QLineEdit.Password)
        self.input_senha.setObjectName("input_senha")

        self.botao_login = QtWidgets.QPushButton(self.main_frame)
        self.botao_login.setGeometry(QtCore.QRect(160, 210, 80, 22))
        self.botao_login.setObjectName("botao_login")
        self.botao_login.setText("Login")

        self.label_mensagem_login = QtWidgets.QLabel(self.main_frame)
        self.label_mensagem_login.setGeometry(QtCore.QRect(70, 240, 231, 21))
        self.label_mensagem_login.setObjectName("label_mensagem_login")
        self.label_mensagem_login.setText("Faça o login ou crie uma nova conta.")

        self.botao_criarConta = QtWidgets.QPushButton(self.main_frame)
        self.botao_criarConta.setGeometry(QtCore.QRect(70, 210, 80, 22))
        self.botao_criarConta.setObjectName("botao_criarConta")
        self.botao_criarConta.setText("Criar Conta")

        self.botao_login.clicked.connect(self.botao_login_pressionado)

        self.main_frame.setVisible(True)

    def remove_login(self):
        sip.delete(self.botao_criarConta)
        self.botao_criarConta = None

        sip.delete(self.label_login)
        self.label_login = None

        sip.delete(self.label_senha)
        self.label_senha = None

        sip.delete(self.input_login)
        self.input_login = None

        sip.delete(self.input_senha)
        self.input_senha = None

        sip.delete(self.label_mensagem_login)
        self.label_mensagem_login = None

        sip.delete(self.botao_login)
        self.botao_login = None

Explaining the code: I initialize the main frame with 0 elements. When a button is pressed, create_login () (in this example) is called and this function creates all the required elements within the frame. This function also uses a control variable for the program to know which frame is currently being displayed, so I use a specific function to remove each frame.

My question is if there is any way to do this the right way? I feel that there is an easier way, I already researched google but it seems that all the ways are with codes nothing like mine.

I'm using QtCreator / QtDesigner to create the layout and using pyuic5 -x test.ui -o test.py in terminal to convert from .ui to .py .

Thank you!

    
asked by anonymous 25.09.2018 / 01:38

0 answers