Updated to use built-in threading call, tuples for thread keys, relative window position
This commit is contained in:
parent
16df165603
commit
ba8b71ea25
|
@ -1,5 +1,4 @@
|
||||||
import PySimpleGUI as sg
|
import PySimpleGUI as sg
|
||||||
import threading
|
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
@ -15,36 +14,45 @@ import time
|
||||||
|
|
||||||
Using the write_event_value method enables you to easily do either of these.
|
Using the write_event_value method enables you to easily do either of these.
|
||||||
|
|
||||||
Copyright 2021 PySimpleGUI
|
In this demo, all thread events are a TUPLE with the first item in tuple being THREAD_KEY ---> '-THEAD-'
|
||||||
|
This allows easy separation of all of the thread-based keys into 1 if statment:
|
||||||
|
elif event[0] == THREAD_KEY:
|
||||||
|
Example
|
||||||
|
(THREAD_KEY, DL_START_KEY) indicates the download is starting and provices the Max value
|
||||||
|
(THREAD_KEY, DL_END_KEY) indicates the downloading has completed
|
||||||
|
|
||||||
|
The main window uses a relative location when making the window so that the one-line-progress-meter has room
|
||||||
|
|
||||||
|
Copyright 2021, 2022 PySimpleGUI
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
THREAD_KEY = '-THREAD-'
|
||||||
DL_START_KEY = '-START DOWNLOAD-'
|
DL_START_KEY = '-START DOWNLOAD-'
|
||||||
DL_COUNT_KEY = '-COUNT-'
|
DL_COUNT_KEY = '-COUNT-'
|
||||||
DL_END_KEY = '-END DOWNLOAD-'
|
DL_END_KEY = '-END DOWNLOAD-'
|
||||||
|
DL_THREAD_EXITNG = '-THREAD EXITING-'
|
||||||
|
|
||||||
def the_thread(window:sg.Window):
|
def the_thread(window:sg.Window):
|
||||||
"""
|
"""
|
||||||
The thread that communicates with the application through the window's events.
|
The thread that communicates with the application through the window's events.
|
||||||
|
|
||||||
Once a second wakes and sends a new event and associated value to the window
|
Simulates downloading a random number of chinks from 50 to 100-
|
||||||
"""
|
"""
|
||||||
max_value = random.randint(50, 100)
|
max_value = random.randint(50, 100)
|
||||||
window.write_event_value(DL_START_KEY, max_value) # Data sent is a tuple of thread name and counter
|
window.write_event_value((THREAD_KEY, DL_START_KEY), max_value) # Data sent is a tuple of thread name and counter
|
||||||
for i in range(max_value):
|
for i in range(max_value):
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
window.write_event_value(DL_COUNT_KEY, i) # Data sent is a tuple of thread name and counter
|
window.write_event_value((THREAD_KEY, DL_COUNT_KEY), i) # Data sent is a tuple of thread name and counter
|
||||||
window.write_event_value(DL_END_KEY, max_value) # Data sent is a tuple of thread name and counter
|
window.write_event_value((THREAD_KEY, DL_END_KEY), max_value) # Data sent is a tuple of thread name and counter
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
layout = [ [sg.Text('My Window')],
|
layout = [ [sg.Text('My Multi-threaded PySimpleGUI Program')],
|
||||||
[sg.ProgressBar(100, 'h', size=(30,20), k='-PROGRESS-')],
|
[sg.ProgressBar(100, 'h', size=(30,20), k='-PROGRESS-', expand_x=True)],
|
||||||
|
[sg.Text(key='-STATUS-')],
|
||||||
[sg.Button('Go'), sg.Button('Exit')] ]
|
[sg.Button('Go'), sg.Button('Exit')] ]
|
||||||
|
|
||||||
window = sg.Window('Window Title', layout, finalize=True)
|
window = sg.Window('Window Title', layout, finalize=True, relative_location=(0, -300))
|
||||||
window.read(timeout=0)
|
|
||||||
window.move(window.current_location()[0], window.current_location()[1]-300)
|
|
||||||
downloading, max_value = False, 0
|
downloading, max_value = False, 0
|
||||||
|
|
||||||
while True: # Event Loop
|
while True: # Event Loop
|
||||||
|
@ -53,17 +61,24 @@ def main():
|
||||||
if event == sg.WIN_CLOSED or event == 'Exit':
|
if event == sg.WIN_CLOSED or event == 'Exit':
|
||||||
break
|
break
|
||||||
if event == 'Go' and not downloading:
|
if event == 'Go' and not downloading:
|
||||||
threading.Thread(target=the_thread, args=(window,), daemon=True).start()
|
window.start_thread(lambda: the_thread(window), (THREAD_KEY, DL_THREAD_EXITNG))
|
||||||
elif event == DL_START_KEY:
|
# Events coming from the Thread
|
||||||
max_value = values[event]
|
elif event[0] == THREAD_KEY:
|
||||||
downloading = True
|
if event[1] == DL_START_KEY:
|
||||||
sg.one_line_progress_meter(f'Downloading {max_value} segments', 0, max_value, 1, f'Downloading {max_value} segments', )
|
max_value = values[event]
|
||||||
window['-PROGRESS-'].update(0, max_value)
|
downloading = True
|
||||||
elif event == DL_COUNT_KEY:
|
window['-STATUS-'].update('Starting download')
|
||||||
sg.one_line_progress_meter(f'Downloading {max_value} segments', values[event]+1, max_value, 1, f'Downloading {max_value} segments')
|
sg.one_line_progress_meter(f'Downloading {max_value} segments', 0, max_value, 1, f'Downloading {max_value} segments', )
|
||||||
window['-PROGRESS-'].update(values[event]+1, max_value)
|
window['-PROGRESS-'].update(0, max_value)
|
||||||
elif event == DL_END_KEY:
|
elif event[1] == DL_COUNT_KEY:
|
||||||
downloading = False
|
sg.one_line_progress_meter(f'Downloading {max_value} segments', values[event]+1, max_value, 1, f'Downloading {max_value} segments')
|
||||||
|
window['-STATUS-'].update(f'Got a new current count update {values[event]}')
|
||||||
|
window['-PROGRESS-'].update(values[event]+1, max_value)
|
||||||
|
elif event[1] == DL_END_KEY:
|
||||||
|
downloading = False
|
||||||
|
window['-STATUS-'].update('Download finished')
|
||||||
|
elif event[1] == DL_THREAD_EXITNG:
|
||||||
|
window['-STATUS-'].update('Last step - Thread has exited')
|
||||||
|
|
||||||
window.close()
|
window.close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue