Skip to content

Instantly share code, notes, and snippets.

@fuji44
Last active June 11, 2021 23:41
Show Gist options
  • Save fuji44/01bedc95a94e2e970acc4b4bd9144b20 to your computer and use it in GitHub Desktop.
Save fuji44/01bedc95a94e2e970acc4b4bd9144b20 to your computer and use it in GitHub Desktop.
Running long-time processes in PySimpleGUI with threads
import threading
import time
import PySimpleGUI as sg
def long_time_process():
time.sleep(5)
def run(window):
window.write_event_value("-PrintStatus-", ("Processing..."))
long_time_process()
window.write_event_value("-PrintStatus-", ("Done!"))
def main():
layout = [
[sg.Text("Text", key="-OUT-", size=(20, 1))],
[sg.Submit("Run thread process")],
]
window = sg.Window("Window Title", layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == "Run thread process":
threading.Thread(target=run, args=(window,), daemon=True).start()
if event == "-PrintStatus-":
window["-OUT-"].update(values["-PrintStatus-"])
window.close()
if __name__ == "__main__":
main()
import threading
import asyncio
import PySimpleGUI as sg
async def long_time_process():
await asyncio.sleep(5)
def run(window):
window.write_event_value("-PrintStatus-", ("Processing..."))
asyncio.run(long_time_process())
window.write_event_value("-PrintStatus-", ("Done!"))
def main():
layout = [
[sg.Text("Text", key="-OUT-", size=(20, 1))],
[sg.Submit("Run thread process")],
]
window = sg.Window("Window Title", layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == "Run thread process":
threading.Thread(target=run, args=(window,), daemon=True).start()
if event == "-PrintStatus-":
window["-OUT-"].update(values["-PrintStatus-"])
window.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment