Added a progress bar
This commit is contained in:
parent
cfaa9990af
commit
235ead0f79
|
@ -1,11 +1,8 @@
|
|||
#!/usr/bin/python3
|
||||
import queue
|
||||
import threading
|
||||
import time
|
||||
import PySimpleGUI as sg
|
||||
|
||||
# This program has been tested on all flavors of PySimpleGUI and it works with no problems at all
|
||||
|
||||
"""
|
||||
DESIGN PATTERN - Multithreaded Long Tasks GUI using shared global variables
|
||||
Presents one method for running long-running operations in a PySimpleGUI environment.
|
||||
|
@ -16,10 +13,11 @@ import PySimpleGUI as sg
|
|||
a Window is created, and an event loop is executed.
|
||||
"""
|
||||
|
||||
|
||||
thread_done = 1
|
||||
message = ''
|
||||
thread_counter = 0
|
||||
total = 100
|
||||
progress = 0
|
||||
|
||||
def long_operation_thread(seconds):
|
||||
"""
|
||||
|
@ -29,11 +27,14 @@ def long_operation_thread(seconds):
|
|||
:return:
|
||||
"""
|
||||
|
||||
global thread_done, message, thread_counter
|
||||
global thread_done, message, thread_counter, progress
|
||||
|
||||
print('Starting thread - will sleep for {} seconds'.format(seconds))
|
||||
thread_counter += 1
|
||||
time.sleep(seconds) # sleep for a while
|
||||
for i in range(int(seconds*10)):
|
||||
time.sleep(.1) # sleep for a while
|
||||
progress += total/(seconds*10)
|
||||
|
||||
message = f'***This is a message from the thread {thread_counter} ***'
|
||||
thread_done = True
|
||||
|
||||
|
@ -44,7 +45,7 @@ def the_gui():
|
|||
Reads data from a global variable and displays
|
||||
Returns when the user exits / closes the window
|
||||
"""
|
||||
global thread_done, message
|
||||
global thread_done, message, progress
|
||||
|
||||
sg.change_look_and_feel('Light Brown 3')
|
||||
|
||||
|
@ -53,6 +54,7 @@ def the_gui():
|
|||
[sg.Text('Number of seconds your task will take'),
|
||||
sg.Input(key='-SECONDS-', size=(5, 1)),
|
||||
sg.Button('Do Long Task', bind_return_key=True)],
|
||||
[sg.Text('Work progress'), sg.ProgressBar(total, size=(20,20), orientation='h', key='-PROG-')],
|
||||
[sg.Button('Click Me'), sg.Button('Exit')], ]
|
||||
|
||||
window = sg.Window('Multithreaded Window', layout)
|
||||
|
@ -72,8 +74,13 @@ def the_gui():
|
|||
if thread_done is True:
|
||||
print('The thread has finished!')
|
||||
print(f'message = {message}')
|
||||
# reset everything for the next run
|
||||
thread_done = False
|
||||
message = ''
|
||||
progress = 0
|
||||
window['-PROG-'].update_bar(total, total) # show the bar as maxed out
|
||||
if progress != 0:
|
||||
window['-PROG-'].update_bar(progress, total) # update the progress bar if non-zero
|
||||
|
||||
# if user exits the window, then close the window and exit the GUI func
|
||||
window.close()
|
||||
|
|
Loading…
Reference in New Issue