How to switch widgets in stacked widget using a button?

后端 未结 1 1487
花落未央
花落未央 2021-01-26 02:35

So I have created a stacked widget with 2 pages. In the first page, I have one button \" Sign In \". In the second page, I have One button \" Logout\"

I want that, if I

1条回答
  •  失恋的感觉
    2021-01-26 03:21

    According to the code you provide, I deduce that you used StackedWidget as a template, so the class you use fills in ui to inherit from that class.

    An error that I see in your code is self.ui.StackedWidget since ui has no attribute called StackedWidget, so you should throw an exception.

    Considering the above, the solution is:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_StackedWidget(object):
        def setupUi(self, StackedWidget):
            # ...
    
        def retranslateUi(self, StackedWidget):
            # ...
    
    
    class StackedWidget(QtWidgets.QStackedWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.ui = Ui_StackedWidget()
            self.ui.setupUi(self)
            self.ui.LogoutBtn.clicked.connect(lambda : self.setCurrentIndex(0))
            self.ui.pushButton.clicked.connect(lambda : self.setCurrentIndex(1))
    
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = StackedWidget()
        w.show()
        sys.exit(app.exec_())
    
    

    0 讨论(0)
提交回复
热议问题