1

MainWindow

I created a Window in which there are 4 different QToolButtons and it is outside QStackedWidget. When I click on the first QToolButton which is Balance Inquiry as shown in the image then the contents of Balance Inquiry should be shown and similarly for the rest of the QToolButtons. Both of them are in the same frame.

I don't know how to connect. I'm learning PyQt5. I'm just using the designer and have a very basic idea about the coding in PyQt5.

GitHub repository link is : https://github.com/abhi7585/Bank-of-Braavos

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • provide a [mre], share the content of .ui – eyllanesc Jan 15 '20 at 16:50
  • Here is the Git Hub repository link https://github.com/abhi7585/Bank-of-Braavos – Abhishek Tripathi Jan 15 '20 at 16:59
  • It seems that you have not understood the question, the MRE must be provided in your question and not depend on an external link since if it breaks it makes your question useless, so edit your question and add it there. Read [ask], [answer] and pass the [tour] – eyllanesc Jan 15 '20 at 17:08

1 Answers1

3

Since you do not provide an MRE then I will provide a solution from scratch.

The solution is to use QButtonGroup where the buttons associated with ids are added, and then use the buttonClicked signal that will send the information of the id of the pressed button that should be set as currentIndex of the QStackedWidget.

import sys

from PyQt5 import QtCore, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.title_label = QtWidgets.QLabel(
            "BANK OF BRAAVOS", alignment=QtCore.Qt.AlignCenter
        )

        self.balance_inquiry_button = QtWidgets.QToolButton(text="Balance Inquiry")
        self.transaction_button = QtWidgets.QToolButton(text="Transaction")
        self.balance_sheet_button = QtWidgets.QToolButton(text="Balance Sheet")
        self.support_button = QtWidgets.QToolButton(text="Support")

        self.stacked_widget = QtWidgets.QStackedWidget()

        self.stacked_widget.addWidget(
            QtWidgets.QLabel("Balance Inquiry", alignment=QtCore.Qt.AlignCenter)
        )
        self.stacked_widget.addWidget(
            QtWidgets.QLabel("Transaction", alignment=QtCore.Qt.AlignCenter)
        )
        self.stacked_widget.addWidget(
            QtWidgets.QLabel("Balance Sheet", alignment=QtCore.Qt.AlignCenter)
        )
        self.stacked_widget.addWidget(
            QtWidgets.QLabel("Support", alignment=QtCore.Qt.AlignCenter)
        )

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(self.title_label)

        hlay = QtWidgets.QHBoxLayout()
        hlay.addWidget(self.balance_inquiry_button)
        hlay.addWidget(self.transaction_button)
        hlay.addWidget(self.balance_sheet_button)
        hlay.addWidget(self.support_button)
        lay.addLayout(hlay)
        lay.addWidget(self.stacked_widget)

        self.group_button = QtWidgets.QButtonGroup()
        for i, button in enumerate(
            (
                self.balance_inquiry_button,
                self.transaction_button,
                self.balance_sheet_button,
                self.support_button,
            )
        ):
            self.group_button.addButton(button, i)

        self.group_button.buttonClicked[int].connect(
            self.stacked_widget.setCurrentIndex
        )

        self.resize(640, 480)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = MainWindow()
    ex.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241