Skip to content

Instantly share code, notes, and snippets.

@MalloyDelacroix
Last active July 4, 2023 09:05
Show Gist options
  • Save MalloyDelacroix/2c509d6bcad35c7e35b1851dfc32d161 to your computer and use it in GitHub Desktop.
Save MalloyDelacroix/2c509d6bcad35c7e35b1851dfc32d161 to your computer and use it in GitHub Desktop.
A PyQt5 example of how to switch between multiple windows.
import sys
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal(str)
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle('Main Window')
layout = QtWidgets.QGridLayout()
self.line_edit = QtWidgets.QLineEdit()
layout.addWidget(self.line_edit)
self.button = QtWidgets.QPushButton('Switch Window')
self.button.clicked.connect(self.switch)
layout.addWidget(self.button)
self.setLayout(layout)
def switch(self):
self.switch_window.emit(self.line_edit.text())
class WindowTwo(QtWidgets.QWidget):
def __init__(self, text):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle('Window Two')
layout = QtWidgets.QGridLayout()
self.label = QtWidgets.QLabel(text)
layout.addWidget(self.label)
self.button = QtWidgets.QPushButton('Close')
self.button.clicked.connect(self.close)
layout.addWidget(self.button)
self.setLayout(layout)
class Login(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle('Login')
layout = QtWidgets.QGridLayout()
self.button = QtWidgets.QPushButton('Login')
self.button.clicked.connect(self.login)
layout.addWidget(self.button)
self.setLayout(layout)
def login(self):
self.switch_window.emit()
class Controller:
def __init__(self):
pass
def show_login(self):
self.login = Login()
self.login.switch_window.connect(self.show_main)
self.login.show()
def show_main(self):
self.window = MainWindow()
self.window.switch_window.connect(self.show_window_two)
self.login.close()
self.window.show()
def show_window_two(self, text):
self.window_two = WindowTwo(text)
self.window.close()
self.window_two.show()
def main():
app = QtWidgets.QApplication(sys.argv)
controller = Controller()
controller.show_login()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
@MalloyDelacroix
Copy link
Author

You're welcome, I'm glad I could help. I'm from the U.S.

@soumilshah1995
Copy link

soumilshah1995 commented Jun 6, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment