2019-07-09 17:09:25 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import time
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2019-07-09 17:09:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
2022-12-19 21:55:58 +00:00
|
|
|
Demo Program - Multithreaded Long Tasks GUI
|
|
|
|
|
2019-07-09 17:09:25 +00:00
|
|
|
Presents one method for running long-running operations in a PySimpleGUI environment.
|
2022-12-19 21:55:58 +00:00
|
|
|
|
2019-07-09 17:09:25 +00:00
|
|
|
The PySimpleGUI code, and thus the underlying GUI framework, runs as the primary, main thread
|
|
|
|
The "long work" is contained in the thread that is being started.
|
|
|
|
|
2022-12-19 21:55:58 +00:00
|
|
|
So that you don't have to import and understand the threading module, this program uses window.start_thread to run a thread.
|
2020-07-18 17:42:32 +00:00
|
|
|
|
2022-12-19 21:55:58 +00:00
|
|
|
The thread is using TUPLES for its keys. This enables you to easily find the thread events by looking at event[0].
|
|
|
|
The Thread Keys look something like this: ('-THREAD-', message)
|
|
|
|
If event [0] == '-THREAD-' then you know it's one of these tuple keys.
|
|
|
|
|
|
|
|
Copyright 2022 PySimpleGUI
|
2019-07-09 17:09:25 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-07-18 17:42:32 +00:00
|
|
|
def long_operation_thread(seconds, window):
|
2019-07-09 17:09:25 +00:00
|
|
|
"""
|
|
|
|
A worker thread that communicates with the GUI through a queue
|
|
|
|
This thread can block for as long as it wants and the GUI will not be affected
|
|
|
|
:param seconds: (int) How long to sleep, the ultimate blocking call
|
2022-12-19 21:55:58 +00:00
|
|
|
:param window: (sg.Window) the window to communicate with
|
2019-07-09 17:09:25 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2022-12-19 21:55:58 +00:00
|
|
|
window.write_event_value(('-THREAD-', 'Starting thread - will sleep for {} seconds'.format(seconds)), None)
|
2019-07-09 17:09:25 +00:00
|
|
|
time.sleep(seconds) # sleep for a while
|
2022-12-19 21:55:58 +00:00
|
|
|
window.write_event_value(('-THREAD-', '** DONE **'), 'Done!') # put a message into queue for GUI
|
2019-07-09 17:09:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def the_gui():
|
|
|
|
"""
|
|
|
|
Starts and executes the GUI
|
|
|
|
Reads data from a Queue and displays the data to the window
|
|
|
|
Returns when the user exits / closes the window
|
|
|
|
"""
|
2019-12-24 23:52:47 +00:00
|
|
|
sg.theme('Light Brown 3')
|
2019-07-09 17:09:25 +00:00
|
|
|
|
|
|
|
layout = [[sg.Text('Long task to perform example')],
|
|
|
|
[sg.Output(size=(70, 12))],
|
2019-10-23 20:10:03 +00:00
|
|
|
[sg.Text('Number of seconds your task will take'),
|
2022-12-19 21:55:58 +00:00
|
|
|
sg.Input(default_text=5, key='-SECONDS-', size=(5, 1)),
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.Button('Do Long Task', bind_return_key=True)],
|
2019-07-09 17:09:25 +00:00
|
|
|
[sg.Button('Click Me'), sg.Button('Exit')], ]
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Multithreaded Window', layout)
|
2019-07-09 17:09:25 +00:00
|
|
|
|
|
|
|
# --------------------- EVENT LOOP ---------------------
|
|
|
|
while True:
|
2020-07-18 17:42:32 +00:00
|
|
|
event, values = window.read()
|
2020-05-07 10:22:59 +00:00
|
|
|
if event in (sg.WIN_CLOSED, 'Exit'):
|
2019-07-09 17:09:25 +00:00
|
|
|
break
|
2022-12-19 21:55:58 +00:00
|
|
|
elif event == 'Do Long Task':
|
2020-07-18 17:42:32 +00:00
|
|
|
seconds = int(values['-SECONDS-'])
|
|
|
|
print('Thread ALIVE! Long work....sending value of {} seconds'.format(seconds))
|
2022-12-19 21:55:58 +00:00
|
|
|
window.start_thread(lambda: long_operation_thread(seconds, window), ('-THREAD-', '-THEAD ENDED-'))
|
2019-07-09 17:09:25 +00:00
|
|
|
elif event == 'Click Me':
|
|
|
|
print('Your GUI is alive and well')
|
2022-12-19 21:55:58 +00:00
|
|
|
elif event[0] == '-THREAD-':
|
|
|
|
print('Got a message back from the thread: ', event[1])
|
2019-07-09 17:09:25 +00:00
|
|
|
|
|
|
|
# if user exits the window, then close the window and exit the GUI func
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|
2019-07-09 17:09:25 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
the_gui()
|
2019-10-23 20:10:03 +00:00
|
|
|
print('Exiting Program')
|