Answer the question
In order to leave comments, you need to log in
How to clear all widgets in PyQt5 (python3)?
Hey!
How can I make it so that when I click on the button (QPushButton) everything in the window is cleared and new content is displayed. Then let's say that by clicking on the "back" button, the old content was displayed.
I hope you understand me...
Answer the question
In order to leave comments, you need to log in
In order not to delete and recreate widgets every time, you can simply hide them and show them as needed.
As an option, make a button click processing slot and change its name (or take another internal variable as a basis, it’s more convenient here), and in the processing slot, depending on the state of the desired variable, simply hide or show the necessary widgets using hide()
and show()
For example, here So:
def initUI(self):
self.barWidget = QWidget()
self.barWidget.show()
self.fooWidget = QWidget()
self.fooWidget.hide()
self.magicButton = QPushButton("foo")
self.magicButton.clicked.connect(lambda: self.check_state_slot(self.magicButton.text()))
@pyqtSlot()
def check_state_slot(self, state):
if state == "foo":
self.barWidget.hide()
self.fooWidget.show()
self.magicButton.setText("bar")
elif state == "bar":
self.fooWidget.hide()
self.barWidget.show()
self.magicButton.setText("foo")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question