PyQT - Switch between windows with StackedWidget

5

I divided the application into parts, but for now I just want it to work when I try to switch between windows (without creating a new one or closing it). I'm doing this with buttons.

Open well without problems and start in the main menu, and there is a button that takes you to the second menu that is the "search menu"; This one works well. Then when you are in the search menu there is another button that will take you back to the main menu, but it does not work.

If you do not understand what I say, execute the code and you will understand everything.

import os, sys
from PyQt4.QtGui import QMessageBox, QStackedWidget
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)

        search_widget = searchWidget(self)
        search_widget.returnButton.clicked.connect(self.returning) #The returnButton from searchWidget(self) connects to the function returning(self), but the returning function does not return to the home menu.
        self.central_widget.addWidget(search_widget)
    def login(self):
        layout = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton('Login')
        layout.addWidget(self.button)
        self.setLayout(layout)
        logged_in_widget = searchWidget(self)
        search_widget = searchWidget(self)
        self.central_widget.addWidget(logged_in_widget) # This part is key
        self.central_widget.setCurrentWidget(logged_in_widget)
    def returning(self):
      # Here what im doing is restoring everything like it was before in the home menu, that's why I put the "Login" button again (in line 31).
      # This is not very optimized but i put it all again so that its more understandable. I can also, instead of all this code (from line 30 to 37), put a call to the login function like this: login(self) but I prefer the first way.
        layout = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton('Login')
        layout.addWidget(self.button)
        self.setLayout(layout)
        logged_in_widget = searchWidget(self)
        search_widget = searchWidget(self)
        self.central_widget.addWidget(logged_in_widget)
        self.central_widget.setCurrentWidget(logged_in_widget)
      # So, i got the buttons there they look fine, but the return button doesn't do anything, while the "login" button (that switchs to the searchWidget) does work.



class LoginWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoginWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton('Login')
        self.label = QtGui.QLabel('You are on the menu now')
        layout.addWidget(self.button)
        layout.addWidget(self.label)
        self.setLayout(layout)
        # you might want to do self.button.click.connect(self.parent().login) here

class searchWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(searchWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.returnButton = QtGui.QPushButton('Return')
        layout.addWidget(self.returnButton)
        self.label = QtGui.QLabel('logged in in searchwidget!!!!')
        layout.addWidget(self.label)
        self.setLayout(layout)


if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()
    
asked by broadwaydannyrose 07.03.2016 в 00:31
source

1 answer

3

I think you're doing too much extra work on your methods, this is the code I've made work:

import os, sys
from PyQt4.QtGui import QMessageBox, QStackedWidget
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
        self.login_widget = LoginWidget(self)
        self.login_widget.button.clicked.connect(self.login)
        self.central_widget.addWidget(self.login_widget)
        # Return
        self.search_widget = SearchWidget(self)
        self.search_widget.returnButton.clicked.connect(self.returning)
        self.central_widget.addWidget(self.search_widget)

    def login(self):
        self.central_widget.setCurrentWidget(self.search_widget)

    def returning(self):
        self.central_widget.setCurrentWidget(self.login_widget)


class LoginWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoginWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton('Login')
        self.label = QtGui.QLabel('You are on the menu now')
        layout.addWidget(self.button)
        layout.addWidget(self.label)
        self.setLayout(layout)

class SearchWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(SearchWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.returnButton = QtGui.QPushButton('Return')
        self.label = QtGui.QLabel('logged in in searchwidget!!!!')
        layout.addWidget(self.returnButton)
        layout.addWidget(self.label)
        self.setLayout(layout)


if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

Notes:

  • See how I defined self.login_widget and self.search_widget in% method __init__ as% attributes MainWindow to be used later in their respective methods
  • In the methods login and returning you only need to specify the Widget current. According to the documentation for the QStackedWidget.setCurrentWidget method, the Widget should already be contained within of the QStackedWidget , and this we have done in the __init__ method:

    self.central_widget.addWidget(self.login_widget)
    self.central_widget.addWidget(self.search_widget)
    
answered by 07.03.2016 / 14:30
source