Skip to content

Commit 6af5d91

Browse files
Merge pull request PySimpleGUI#1406 from PySimpleGUI/Dev-latest
New Demo program showing long tasks
2 parents 794f6cd + 2887887 commit 6af5d91

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

DemoPrograms/Demo_Threaded_Work.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/python3
2+
import queue
3+
import threading
4+
import time
5+
6+
import PySimpleGUI as sg
7+
8+
# Put your long-running code in here
9+
def worker_thread(thread_name, gui_queue):
10+
print('Starting thread - {} '.format(thread_name))
11+
# this is our "long running function call"
12+
time.sleep(5) # sleep for a while
13+
14+
# at the end of the work, before exiting, send a message back to the GUI indicating end
15+
gui_queue.put('{} - done'.format(thread_name)) # put a message into queue for GUI
16+
17+
18+
###### ## ## ####
19+
## ## ## ## ##
20+
## ## ## ##
21+
## #### ## ## ##
22+
## ## ## ## ##
23+
## ## ## ## ##
24+
###### ####### ####
25+
26+
def the_gui(gui_queue):
27+
28+
layout = [[sg.Text('Multithreaded Work Example')],
29+
[sg.Text('', size=(25, 1), key='_OUTPUT_')],
30+
# [sg.Output(size=(40,6))],
31+
[sg.Button('Go'), sg.Button('Exit')], ]
32+
33+
window = sg.Window('Multithreaded Window').Layout(layout)
34+
# --------------------- EVENT LOOP ---------------------
35+
message = None
36+
while True:
37+
event, values = window.Read(timeout=100) # wait for up to 100 ms for a GUI event
38+
if event is None or event == 'Exit':
39+
break
40+
if event == 'Go':
41+
window.Element('_OUTPUT_').Update('Starting long work....')
42+
# simulate STARTING long run by starting a thread
43+
threading.Thread(target=worker_thread, args=('Thread 1', gui_queue,), daemon=True).start()
44+
45+
# --------------- Loop through all messages coming in from threads ---------------
46+
try: # see if something has been posted to Queue
47+
message = gui_queue.get_nowait()
48+
except queue.Empty: # get_nowait() will get exception when Queue is empty
49+
pass # nothing in queue so do nothing
50+
51+
# if message received from queue, display the message in the Window
52+
if message is not None and message.startswith('Thread 1'):
53+
# this is the place you would execute code at ENDING of long running task
54+
window.Element('_OUTPUT_').Update(message)
55+
window.Refresh() # do a refresh because could be showing multiple messages before next Read
56+
message = None
57+
58+
# if user exits the window, then close the window and exit the GUI func
59+
window.Close()
60+
61+
62+
## ## ### #### ## ##
63+
### ### ## ## ## ### ##
64+
#### #### ## ## ## #### ##
65+
## ### ## ## ## ## ## ## ##
66+
## ## ######### ## ## ####
67+
## ## ## ## ## ## ###
68+
## ## ## ## #### ## ##
69+
70+
if __name__ == '__main__':
71+
# -- Create a Queue to communicate with GUI --
72+
gui_queue = queue.Queue() # queue used to communicate between the gui and the threads
73+
the_gui(gui_queue)
74+
print('Exiting Program')

0 commit comments

Comments
 (0)