PyQt - PushButton Hide and Show works not as excepted

23 views Asked by At

I have a simple example to hide/show button and in show mode, everything works as expected. Ok

In hide mode, I excepted that the "Toggle" button only to be displayed [ok], but the size of the "Toggle" button is wider that the original one. I excepted also, that the main windows is being resized as well.

enter image description here

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.layout = QHBoxLayout(self.centralWidget)
        self.button1 = QPushButton("Hide-1") # Button-1
        self.layout.addWidget(self.button1)
        self.button2 = QPushButton("Hide-2") # Button-2
        self.layout.addWidget(self.button2)
        self.button3 = QPushButton("Toggle")     # Button-
        self.button3.clicked.connect(self.hide_unhide)
        self.layout.addWidget(self.button3)
    def hide_unhide(self):
        if self.button1.isVisible():
            self.button1.hide()
            self.button2.hide()
        else:
            self.button1.show()
            self.button2.show()
        self.adjustSize()               # This doesn't work
        self.centralWidget.adjustSize() # This doesn't work

app= QApplication(sys.argv) 
window = MainWindow() 
window.show() 
app.exec()
0

There are 0 answers