How do I switch layouts in a window using PyQt?? (Without closing/opening windows)

后端 未结 1 1934
心在旅途
心在旅途 2020-12-29 17:43

I am currently attempting to create a program using python and PyQt4 (not Qt Designer). I created a login class (QDialog) and a Homepage class (QMainWindow). However, becaus

相关标签:
1条回答
  • 2020-12-29 18:31
    from PyQt4 import QtCore, QtGui
    
    class MainWindow(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.central_widget = QtGui.QStackedWidget()
            self.setCentralWidget(self.central_widget)
            login_widget = LoginWidget(self)
            login_widget.button.clicked.connect(self.login)
            self.central_widget.addWidget(login_widget)
        def login(self):
            logged_in_widget = LoggedWidget(self)
            self.central_widget.addWidget(logged_in_widget)
            self.central_widget.setCurrentWidget(logged_in_widget)
    
    
    class LoginWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            super(LoginWidget, self).__init__(parent)
            layout = QtGui.QHBoxLayout()
            self.button = QtGui.QPushButton('Login')
            layout.addWidget(self.button)
            self.setLayout(layout)
            # you might want to do self.button.click.connect(self.parent().login) here
    
    
    class LoggedWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            super(LoggedWidget, self).__init__(parent)
            layout = QtGui.QHBoxLayout()
            self.label = QtGui.QLabel('logged in!')
            layout.addWidget(self.label)
            self.setLayout(layout)
    
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication([])
        window = MainWindow()
        window.show()
        app.exec_()
    
    0 讨论(0)
提交回复
热议问题