Merge pull request #2217 from PySimpleGUI/Dev-latest

Dev latest
This commit is contained in:
PySimpleGUI 2019-11-19 11:25:48 -05:00 committed by GitHub
commit 2bf98cfe12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 13 deletions

View File

@ -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 A worker thread that communicates with the GUI
These threads can call functions that block withouth affecting the GUI (a good thing) 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 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 thread_name: Text name used for displaying info
:param run_freq: How often the thread should run in milliseconds :param run_freq: How often the thread should run in milliseconds
:param gui_queue: Queue used to communicate with the GUI :param gui_queue: Queue used to communicate with the GUI
:return: :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 for i in itertools.count(): # loop forever, keeping count in i as it loops
time.sleep(run_freq/1000) # sleep for a while time.sleep(run_freq/1000) # sleep for a while
# put a message into queue for GUI # put a message into queue for GUI
gui_queue.put('{} - {}'.format(thread_name, i)) 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 -- # -- Create a Queue to communicate with GUI --
# queue used to communicate between the gui and the threads # queue used to communicate between the gui and the threads
gui_queue = queue.Queue() gui_queue = queue.Queue()
# -- Start worker threads, one runs twice as often as the other # -- Start worker threads, each taking a different amount of time
threading.Thread(target=worker_thread, args=( threading.Thread(target=worker_thread1, args=(
'Thread 1', 500, gui_queue,), daemon=True).start() '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() '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() 'Thread 3', 1000, gui_queue,), daemon=True).start()
# -- Start the GUI passing in the Queue -- # -- Start the GUI passing in the Queue --
the_gui(gui_queue) the_gui(gui_queue)

View File

@ -61,8 +61,7 @@ def the_gui():
[sg.Text('Click Go to start a long-running function call')], [sg.Text('Click Go to start a long-running function call')],
[sg.Text('', size=(25, 1), key='-OUTPUT-')], [sg.Text('', size=(25, 1), key='-OUTPUT-')],
[sg.Text('', size=(25, 1), key='-OUTPUT2-')], [sg.Text('', size=(25, 1), key='-OUTPUT2-')],
[sg.Graph((10, 10), (0, 0), (10, 10), [sg.Text(' ',size=(2,1), background_color='blue', key=i) for i in range(20)],
background_color='black', key=i) for i in range(20)],
[sg.Button('Go'), sg.Button('Popup'), sg.Button('Exit')], ] [sg.Button('Go'), sg.Button('Popup'), sg.Button('Exit')], ]
window = sg.Window('Multithreaded Window', layout) window = sg.Window('Multithreaded Window', layout)
@ -102,7 +101,7 @@ def the_gui():
window[completed_work_id].update(background_color='green') window[completed_work_id].update(background_color='green')
if event == 'Popup': 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 # if user exits the window, then close the window and exit the GUI func
window.close() window.close()

View File

@ -6,13 +6,13 @@ import imwatchingyou
This enables you to have a live debugger / REPL in a running PySimpleGUIQt program This enables you to have a live debugger / REPL in a running PySimpleGUIQt program
""" """
layout = [[sg.Text('My PySimpleGUIQt layout')], 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) window = sg.Window('My window', layout)
counter = 0 # something to see updating in the popout window counter = 0 # something to see updating in the popout window
while True: while True:
event, values = window.Read(timeout=100) event, values = window.read(timeout=100)
if event is None: if event is None:
break break
if event == 'Debugger': if event == 'Debugger':