Created
March 6, 2016 19:13
-
-
Save anonymous/21b4837affe45a9a5a62 to your computer and use it in GitHub Desktop.
PyQt - Changing between windows with StackedWidget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment