From 79e02c57261e4fa52f06297fa0f2e2a55c83fd65 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Tue, 19 Nov 2019 11:24:50 -0500 Subject: [PATCH 1/2] Rename and rework of 2 multithreaded demos --- ...> Demo_Multithreaded_Different_Threads.py} | 52 ++++++++++++++++--- ...=> Demo_Multithreaded_Multiple_Threads.py} | 5 +- 2 files changed, 46 insertions(+), 11 deletions(-) rename DemoPrograms/{Demo_Multithreaded_Queued.py => Demo_Multithreaded_Different_Threads.py} (68%) rename DemoPrograms/{Demo_Threaded_Work.py => Demo_Multithreaded_Multiple_Threads.py} (96%) diff --git a/DemoPrograms/Demo_Multithreaded_Queued.py b/DemoPrograms/Demo_Multithreaded_Different_Threads.py similarity index 68% rename from DemoPrograms/Demo_Multithreaded_Queued.py rename to DemoPrograms/Demo_Multithreaded_Different_Threads.py index b4ec9bb8..937d5965 100644 --- a/DemoPrograms/Demo_Multithreaded_Queued.py +++ b/DemoPrograms/Demo_Multithreaded_Different_Threads.py @@ -35,22 +35,58 @@ import PySimpleGUI as sg # ## ## ## ## ## ## ## ## ## ## # ## ## ## ## ## ######## ## ## ######## -def worker_thread(thread_name, run_freq, gui_queue): +def worker_thread1(thread_name, run_freq, gui_queue): """ - A worker thrread that communicates with the GUI - These threads can call functions that block withouth affecting the GUI (a good thing) + A worker thread that communicates with the GUI + These threads can call functions that block without affecting the GUI (a good thing) Note that this function is the code started as each thread. All threads are identical in this way :param thread_name: Text name used for displaying info :param run_freq: How often the thread should run in milliseconds :param gui_queue: Queue used to communicate with the GUI :return: """ - print('Starting thread - {} that runs every {} ms'.format(thread_name, run_freq)) + print('Starting thread 1 - {} that runs every {} ms'.format(thread_name, run_freq)) for i in itertools.count(): # loop forever, keeping count in i as it loops time.sleep(run_freq/1000) # sleep for a while # put a message into queue for GUI gui_queue.put('{} - {}'.format(thread_name, i)) + +def worker_thread2(thread_name, run_freq, gui_queue): + """ + A worker thread that communicates with the GUI + These threads can call functions that block without affecting the GUI (a good thing) + Note that this function is the code started as each thread. All threads are identical in this way + :param thread_name: Text name used for displaying info + :param run_freq: How often the thread should run in milliseconds + :param gui_queue: Queue used to communicate with the GUI + :return: + """ + print('Starting thread 2 - {} that runs every {} ms'.format(thread_name, run_freq)) + for i in itertools.count(): # loop forever, keeping count in i as it loops + time.sleep(run_freq/1000) # sleep for a while + # put a message into queue for GUI + gui_queue.put('{} - {}'.format(thread_name, i)) + + +def worker_thread3(thread_name, run_freq, gui_queue): + """ + A worker thread that communicates with the GUI + These threads can call functions that block without affecting the GUI (a good thing) + Note that this function is the code started as each thread. All threads are identical in this way + :param thread_name: Text name used for displaying info + :param run_freq: How often the thread should run in milliseconds + :param gui_queue: Queue used to communicate with the GUI + :return: + """ + print('Starting thread 3 - {} that runs every {} ms'.format(thread_name, run_freq)) + for i in itertools.count(): # loop forever, keeping count in i as it loops + time.sleep(run_freq/1000) # sleep for a while + # put a message into queue for GUI + gui_queue.put('{} - {}'.format(thread_name, i)) + + + # ###### ## ## #### # ## ## ## ## ## # ## ## ## ## @@ -109,12 +145,12 @@ if __name__ == '__main__': # -- Create a Queue to communicate with GUI -- # queue used to communicate between the gui and the threads gui_queue = queue.Queue() - # -- Start worker threads, one runs twice as often as the other - threading.Thread(target=worker_thread, args=( + # -- Start worker threads, each taking a different amount of time + threading.Thread(target=worker_thread1, args=( 'Thread 1', 500, gui_queue,), daemon=True).start() - threading.Thread(target=worker_thread, args=( + threading.Thread(target=worker_thread2, args=( 'Thread 2', 200, gui_queue,), daemon=True).start() - threading.Thread(target=worker_thread, args=( + threading.Thread(target=worker_thread3, args=( 'Thread 3', 1000, gui_queue,), daemon=True).start() # -- Start the GUI passing in the Queue -- the_gui(gui_queue) diff --git a/DemoPrograms/Demo_Threaded_Work.py b/DemoPrograms/Demo_Multithreaded_Multiple_Threads.py similarity index 96% rename from DemoPrograms/Demo_Threaded_Work.py rename to DemoPrograms/Demo_Multithreaded_Multiple_Threads.py index b8b5f77e..c7d2472a 100644 --- a/DemoPrograms/Demo_Threaded_Work.py +++ b/DemoPrograms/Demo_Multithreaded_Multiple_Threads.py @@ -61,8 +61,7 @@ def the_gui(): [sg.Text('Click Go to start a long-running function call')], [sg.Text('', size=(25, 1), key='-OUTPUT-')], [sg.Text('', size=(25, 1), key='-OUTPUT2-')], - [sg.Graph((10, 10), (0, 0), (10, 10), - background_color='black', key=i) for i in range(20)], + [sg.Text(' ',size=(2,1), background_color='blue', key=i) for i in range(20)], [sg.Button('Go'), sg.Button('Popup'), sg.Button('Exit')], ] window = sg.Window('Multithreaded Window', layout) @@ -102,7 +101,7 @@ def the_gui(): window[completed_work_id].update(background_color='green') if event == 'Popup': - sg.popup('This is a popup showing that the GUI is running') + sg.popup_non_blocking('This is a popup showing that the GUI is running', grab_anywhere=True) # if user exits the window, then close the window and exit the GUI func window.close() From 2cfd2f19b1bc0cf96c290b1bcb612837992bb0d8 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Tue, 19 Nov 2019 11:25:27 -0500 Subject: [PATCH 2/2] Update with PEP8 read call. --- PySimpleGUIQt/Demo Programs/Qt_Demo_imwatchingyou.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PySimpleGUIQt/Demo Programs/Qt_Demo_imwatchingyou.py b/PySimpleGUIQt/Demo Programs/Qt_Demo_imwatchingyou.py index 9f64ce21..ec1ccbd7 100644 --- a/PySimpleGUIQt/Demo Programs/Qt_Demo_imwatchingyou.py +++ b/PySimpleGUIQt/Demo Programs/Qt_Demo_imwatchingyou.py @@ -6,13 +6,13 @@ import imwatchingyou This enables you to have a live debugger / REPL in a running PySimpleGUIQt program """ layout = [[sg.Text('My PySimpleGUIQt layout')], - [sg.Button('OK'), sg.Button('Debugger'), sg.B('Popout')]] + [sg.B('OK'), sg.B('Debugger'), sg.B('Popout')]] window = sg.Window('My window', layout) counter = 0 # something to see updating in the popout window while True: - event, values = window.Read(timeout=100) + event, values = window.read(timeout=100) if event is None: break if event == 'Debugger':