Last active
July 4, 2023 09:05
-
-
Save MalloyDelacroix/2c509d6bcad35c7e35b1851dfc32d161 to your computer and use it in GitHub Desktop.
A PyQt5 example of how to switch between multiple windows.
This file contains 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 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() |
You're welcome, I'm glad I could help. I'm from the U.S.
Even I am from United States I reside in Bridgeport
Would love to meet you if you are near by
On Thu, Jun 6, 2019 at 7:49 AM MalloyDelacroix ***@***.***> wrote:
You're welcome, I'm glad I could help. I'm from the U.S.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/2c509d6bcad35c7e35b1851dfc32d161?email_source=notifications&email_token=AJMF5P7QQZYMIP3P7J2HAWDPZD2UDA5CNFSM4HQPAWYKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFTH3G#gistcomment-2936755>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AJMF5P2PO3GEZHJ663KOOQ3PZD2UDANCNFSM4HQPAWYA>
.
--
Thanking You,
Soumil Nitin Shah
B.E in Electronic
MS Electrical Engineering
MS Computer Engineering
+1-646 204 5957
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello there,
First of all, i want to thank you for such a quick Response I didn't expect such a quick response. I want to say thanks to you for taking out time and reading my code and helping me out a very handful of programmers do that. thanks for the advice that I should not write any code in that instead create a class that inherits from above two class.
Once again I am saying thanks to you
Just curious to know from which country are you from?