Pyside2 // How to change frame without hiding buttons?

Asked

Viewed 16 times

-1

I created a window with 4 frames that alternate when clicking the corresponding buttons, but when clicking any button, the frame changes to the corresponding one but the buttons disappear, where did I miss? Can you help me?

Follows the code:

from PySide2.QtWidgets import QApplication, QWidget,QPushButton, QFrame
from PySide2.QtGui import QFont
import sys
class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Menu')
        self.setGeometry(250,150,800,600)
        self.setToolTip('Menu')
        self.setStyleSheet('background-color: black')

        self.define_formulario()


    def define_formulario(self):

    # DEFINIÇÃO DE FONTE ----------------------------------------
        self.fonte = QFont('fontes/Montserrat-Bold.ttf')
        self.fonte.setPointSize(12)

    # BOTÕES ----------------------------------------------------

        self.btn_cadastrar = QPushButton('Cadastrar', self)
        self.btn_cadastrar.setFont(self.fonte)
        self.btn_cadastrar.setGeometry(0,0,170,50)
        self.btn_cadastrar.setStyleSheet('background-color: #99ff66')
        self.btn_cadastrar.clicked.connect(self.frame_cadastrar)

        self.btn_pesquisar = QPushButton('Pesquisar', self)
        self.btn_pesquisar.setFont(self.fonte)
        self.btn_pesquisar.setGeometry(0, 50, 170, 50)
        self.btn_pesquisar.setStyleSheet('background-color: #99ff66')
        self.btn_pesquisar.clicked.connect(self.frame_pesquisar)

        self.btn_relatorio = QPushButton('Relatório', self)
        self.btn_relatorio.setFont(self.fonte)
        self.btn_relatorio.setGeometry(0, 100, 170, 50)
        self.btn_relatorio.setStyleSheet('background-color: #99ff66')
        self.btn_relatorio.clicked.connect(self.frame_relatorio)

        self.btn_editar = QPushButton('Editar', self)
        self.btn_editar.setFont(self.fonte)
        self.btn_editar.setGeometry(0, 150, 170, 50)
        self.btn_editar.setStyleSheet('background-color: #99ff66')
        self.btn_editar.clicked.connect(self.frame_editar)

    # FRAME CADASTRAR ---------------------------------------------

        global frm_cadastrar
        self.frm_cadastrar = QFrame(self)
        self.frm_cadastrar.setGeometry(170, 0, 630, 600)
        self.frm_cadastrar.setStyleSheet('background-color: white')
        self.frm_cadastrar.setVisible(False)

    # FRAME PESQUISAR ---------------------------------------------

        global frm_pesquisar
        self.frm_pesquisar = QFrame(self)
        self.frm_pesquisar.setGeometry(170, 0, 630, 600)
        self.frm_pesquisar.setStyleSheet('background-color: green')
        self.frm_pesquisar.setVisible(False)

    # FRAME RELATORIO ---------------------------------------------

        global frm_relatorio
        self.frm_relatorio = QFrame(self)
        self.frm_relatorio.setGeometry(170, 0, 630, 600)
        self.frm_relatorio.setStyleSheet('background-color: yellow')
        self.frm_relatorio.setVisible(False)

    # FRAME EDITAR ------------------------------------------------

        global frm_editar
        self.frm_editar = QFrame(self)
        self.frm_editar.setGeometry(170, 0, 630, 600)
        self.frm_editar.setStyleSheet('background-color: red')
        self.frm_editar.setVisible(False)

    # OCULTAÇÃO DE FRAMES -----------------------------------------

        global meus_frames
        self.meus_frames = (self.frm_cadastrar,self.btn_pesquisar,
                            self.btn_relatorio, self.btn_editar)

    def ocultar_frm(self):
        global meus_frames
        for f in self.meus_frames:
            if f.isVisible() == True:
                f.setVisible(False)

# FUNÇÕES DE FRAMES
    def frame_cadastrar(self):
        global frm_cadastrar
        self.ocultar_frm()
        self.frm_cadastrar.setVisible(True)

    def frame_pesquisar(self):
        global frm_pesquisar
        self.ocultar_frm()
        self.frm_pesquisar.setVisible(True)

    def frame_relatorio(self):
        global frm_relatorio
        self.ocultar_frm()
        self.frm_relatorio.setVisible(True)

    def frame_editar(self):
        global frm_editar
        self.ocultar_frm()
        self.frm_editar.setVisible(True)

def executa():
    myApp = QApplication.instance()
    if myApp is None:
        myApp = QApplication(sys.argv)

    window = Window()
    window.show()

    myApp.exec_()

executa()

1 answer

0


In the list of frames put the buttons instead of the frames.

self.meus_frames = (self.frm_cadastrar,self.frm_pesquisar,
                    self.frm_relatorio, self.frm_editar)
  • Thank you very much, I hadn’t realized this mistake, my inattention

Browser other questions tagged

You are not signed in. Login or sign up in order to post.