PySimpleGUI 2020 Part 8 – Async Windows for Periodic Event Loop Execution – EXPERIMENTAL SPEED BOOST

Posted by


In this tutorial, we will be exploring the usage of async windows in PySimpleGUI 2020. Async windows allow you to create windows that run in a separate thread and periodically run the event loop without blocking the main program flow. This feature is particularly useful for applications that require continuous updates or real-time processing.

Please note that async windows are still an experimental feature and may not be fully stable in all scenarios. It is recommended to use this feature with caution and thoroughly test your application before deploying it in a production environment.

To get started with async windows in PySimpleGUI 2020, you will need to install the latest version of PySimpleGUI and import the necessary modules:

import PySimpleGUI as sg
import asyncio

Next, we will create a simple async window that periodically updates a label with the current time:

async def update_time(window):
    while True:
        current_time = sg.popup_get_time()
        window.write_event_value('-UPDATE-', current_time)
        await asyncio.sleep(1)

In this code snippet, we define an async function update_time that takes a window object as an argument. The function runs in an infinite loop, updating the value of the label in the window with the current time every second. We use the write_event_value method to update the window from the separate thread.

Now, let’s create a layout for our window with a label that will display the current time:

layout = [[sg.Text('', key='-TIME-')]]
window = sg.Window('Async Window', layout, finalize=True)

async def main(window):
    async for event in window.async_event_loop():
        if event == sg.WIN_CLOSED:
            break
        elif event == '-UPDATE-':
            window['-TIME-'].update(event.get('-UPDATE-'))

asyncio.create_task(update_time(window))
asyncio.create_task(main(window))

In this code snippet, we create a simple layout with a single label that will display the current time. We then create an async function main that listens for events in the window using the async_event_loop method. When the window is closed, the event loop is terminated. When the -UPDATE- event is triggered, the label in the window is updated with the new value.

Finally, we use the create_task method from the asyncio module to run our update_time and main functions in separate tasks. This allows the window to update the label with the current time while still responding to events from the main program flow.

And that’s it! You have successfully created an async window in PySimpleGUI 2020 that periodically updates a label with the current time. Experiment with this feature and explore its capabilities to create more dynamic and responsive user interfaces in your applications.

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@jonathanmoore5619
23 days ago

Love the videos, wrappers and product. I've been using wx et al… So, this looks so much easier. Can I ask; outside of simple games… You mentioned sudoku… Any chance your GUI could be linked as a front end / menu driver etc for multi windows; for real-time charts, games (sdl2, sfml windows or gl) etc? You've used a sort of basic game (event) loop here… I noticed. But I'm not sure re delta time etc and any consequential performance issues..? or coupling between different apps and language. I guess matlatplotlib and gnuplot (command line) would be easy enough….? Going to spend sometime taking a deeper look, but thanks for the videos etc and any pointers… Pardon the pun, would be appreciated.

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