Creating Multiple Windows in Python Using PySimpleGUI – Tutorial 5

Posted by


In this tutorial, we will be exploring how to create multiple windows using Python and the PySimpleGUI library. PySimpleGUI is a simple yet powerful GUI library for Python that allows you to build user interfaces quickly and easily.

To begin with, make sure you have PySimpleGUI installed on your system. You can install it using pip by running the following command:

pip install PySimpleGUI

Once you have PySimpleGUI installed, let’s start by creating a basic GUI window using PySimpleGUI. You can follow the code below to create a simple window that displays a text message:

import PySimpleGUI as sg

layout = [[sg.Text('Hello, world!')]]

window = sg.Window('My First Window', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break

window.close()

Now that you have a basic understanding of how to create a single window with PySimpleGUI, let’s move on to creating multiple windows. To create multiple windows, you can simply create multiple instances of the sg.Window class.

Here’s an example code snippet that demonstrates how to create two windows with PySimpleGUI:

import PySimpleGUI as sg

layout1 = [[sg.Text('Window 1')]]
layout2 = [[sg.Text('Window 2')]]

window1 = sg.Window('Window 1', layout1)
window2 = sg.Window('Window 2', layout2)

while True:
    event, values = window1.read()
    if event == sg.WIN_CLOSED:
        break

while True:
    event, values = window2.read()
    if event == sg.WIN_CLOSED:
        break

window1.close()
window2.close()

In the code above, we create two windows, window1 and window2, each with a simple text message. We then create an infinite loop for each window that waits for events from the user. Once the user closes the window, the loop breaks and the window is closed.

You can create as many windows as you like by simply creating additional instances of the sg.Window class. Each window will have its own event loop and can contain different layouts and functionality.

That’s it! You have learned how to create multiple windows using Python and PySimpleGUI. Experiment with different layouts and widgets to create more complex user interfaces. Happy coding!

0 0 votes
Article Rating

Leave a Reply

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@turtlecode
21 days ago

✅ Python PySimpleGUI Full Playlist : https://www.youtube.com/playlist?list=PLMi6KgK4_mk1kGWXRif1naLejNdYDS_t2

@josinaldoaraujo3047
21 days ago

Thanks for this video, I'm from Brazil and I'm learnig english and python. Your videos are help me in the two things.

@abdussalam3795
21 days ago

Flash software making video required please

3
0
Would love your thoughts, please comment.x
()
x