Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't make PySimpleGUI calls from threads.... #2

Closed
PySimpleGUI opened this issue Jul 19, 2022 · 1 comment
Closed

Can't make PySimpleGUI calls from threads.... #2

PySimpleGUI opened this issue Jul 19, 2022 · 1 comment
Assignees

Comments

@PySimpleGUI
Copy link

It appears you're making PySimpleGUI calls from a thread. You may even be "getting away with it". If tktiner catches you, your program will crash. PySimpleGUI wasn't written to be threadsafe either.

There is an easy way out however. window.write_event_value() is callable from a thread. With this call you can pass data through to your event loop and then have your event loop perform the operation. I recommend using tuples for this. If you use a tuple with your events, then you can have the first item in your tuple be an indicator that the event is from your thread, then have the next item indicate the operation.

Maybe an event like this if you want to print something to the element with the key '-ML-':
('-THREAD-', 'print', '-ML-')

window.write_event_value(('-THREAD-', 'print', '-ML-'), "this is what I want to print")

In your event loop you can then have

if event[0] == '-THREAD-':
    # all of your thread based event processing...
    if event[1] == 'print':
        window[event[2]].print(values[event])

I did something like this when I added threading to an image restoration project I stumbled onto:
mizosoft/ImageRestorationAndEnhancement#2

Lots of different ways to do it. You'll find examples in the Demo Programs as well that use write_event_value
image

@Aadv1k
Copy link
Owner

Aadv1k commented Jul 20, 2022

Would love to get to working on this issue ASAP, however currently my exams are going so unfortunately it has to wait. Thanks for pointing it out! I actually learnt of this after I had implemented everything, so I got a bit lazy and just never came down to doing it.

@Aadv1k Aadv1k self-assigned this Aug 4, 2022
@Aadv1k Aadv1k closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants