Removed cprint from thread. Added dummy button

This commit is contained in:
PySimpleGUI 2020-07-18 15:36:12 -04:00
parent e6d0eca440
commit 110e5a0daf
1 changed files with 12 additions and 13 deletions

View File

@ -1,8 +1,8 @@
import threading import threading
import time import time
import PySimpleGUI as sg import PySimpleGUI as sg
""" """
Threaded Demo - Uses Window.write_event_value communications Threaded Demo - Uses Window.write_event_value communications
@ -30,8 +30,7 @@ def the_thread(window):
i = 0 i = 0
while True: while True:
time.sleep(1) time.sleep(1)
sg.cprint(f'thread info (in thread) = {threading.current_thread().name}', c='white on purple') window.write_event_value('-THREAD-', (threading.current_thread().name, i)) # Data sent is a tuple of thread name and counter
window.write_event_value('-THREAD-', i)
i += 1 i += 1
@ -40,27 +39,27 @@ def main():
The demo will display in the multiline info about the event and values dictionary as it is being The demo will display in the multiline info about the event and values dictionary as it is being
returned from window.read() returned from window.read()
Every time "Start" is clicked a new thread is started Every time "Start" is clicked a new thread is started
Try clicking "go" to see that the window is active while the thread stuff is happening in the background Try clicking "Dummy" to see that the window is active while the thread stuff is happening in the background
""" """
layout = [ [sg.Text('My Window')], layout = [ [sg.Text('Output Area - cprint\'s route to here', font='Any 15')],
[sg.Multiline(size=(40,20), key='-ML-', autoscroll=True, reroute_stdout=True, write_only=True, reroute_cprint=True)], [sg.Multiline(size=(65,20), key='-ML-', autoscroll=True, reroute_stdout=True, write_only=True, reroute_cprint=True)],
[sg.Input(key='-IN-')], [sg.T('Input so you can see data in your dictionary')],
[sg.Button('Go'), sg.B('Start'), sg.Button('Exit')] ] [sg.Input(key='-IN-', size=(30,1))],
[sg.B('Start A Thread'), sg.B('Dummy'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout) window = sg.Window('Window Title', layout)
while True: # Event Loop while True: # Event Loop
event, values = window.read() event, values = window.read()
sg.cprint(event, values) sg.cprint(event, values)
sg.cprint(f'thread info = {threading.current_thread().name}')
if event == sg.WIN_CLOSED or event == 'Exit': if event == sg.WIN_CLOSED or event == 'Exit':
break break
if event == 'Start': if event.startswith('Start'):
thread = threading.Thread(target=the_thread, args=(window,), daemon=True) threading.Thread(target=the_thread, args=(window,), daemon=True).start()
thread.start()
if event == THREAD_EVENT: if event == THREAD_EVENT:
sg.cprint(f'Data from the thread = {values[THREAD_EVENT]}', colors='white on red') sg.cprint(f'Data from the thread ', colors='white on purple', end='')
sg.cprint(f'{values[THREAD_EVENT]}', colors='white on red')
window.close() window.close()