Last active
June 11, 2021 23:41
-
-
Save fuji44/01bedc95a94e2e970acc4b4bd9144b20 to your computer and use it in GitHub Desktop.
Running long-time processes in PySimpleGUI with threads
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 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() |
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 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