I am creating a GUI with a stacked widget and a side bar with some amount of buttons. The end user is supposed to dynamically create new buttons for the side bar and new pages for the stacked widget, as well as be able to change the pages in the stacked widget using the buttons created. I am having no issue creating the buttons and the widget pages, the problem comes from connecting the specific button to the specific page.
Whenever the user creates a new page and button, the button is added to a dict with the button identifier as the key. In the init function of the MainWindow class I call check_button(), which is supposed to connect all the button objects inside the dict to match their page in the stacked widget. However when I run the application and click the buttons, the stacked widget only shows the last page it has indexed for all the buttons. I will admit I am a bit of a newbie when it comes to PySide/PyQt so any help would be appreciated :)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
# Dict with buttons
self.menu_buttons = {}
self.setupUi(self)
# Function to check if buttons have been clicked
self.check_button()
def check_button(self):
for index, button in enumerate(self.menu_buttons.values()):
print(button)
print(index)
button.clicked.connect(lambda: self.change_page(button, index))
def change_page(self, button_page, index):
#print(index)
#print(button_page)
self.content.setCurrentIndex(index)
# This function is called every time the user creates a new button and widget page
# Note that the function that calls this is cut out from the sample provided
def add_config(self, cluster_name, username, password, nodes):
self.menu_buttons[cluster_name] = QPushButton(self.menu_bar)
...
self.cluster_page = QtWidgets.QWidget()
...
self.content.addWidget(self.cluster_page)