Skip to content

Commit 87f4330

Browse files
Added Manually Updated example. New comments, PEP8 func names.
1 parent 858d73f commit 87f4330

1 file changed

Lines changed: 61 additions & 13 deletions

File tree

DemoPrograms/Demo_Progress_Meters.py

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
The simple case is that you want to add a single meter to your code. The one-line solution
2525
"""
2626

27-
def DemoOneLineProgressMeter():
27+
def demo_one_line_progress_meter():
2828
# Display a progress meter. Allow user to break out of loop using cancel button
29-
for i in range(1000):
30-
if not sg.OneLineProgressMeter('My 1-line progress meter', i+1, 1000, 'meter key' ):
29+
for i in range(10000):
30+
if not sg.OneLineProgressMeter('My 1-line progress meter', i+1, 10000, 'meter key','MY MESSAGE1', 'MY MESSAGE 2', orientation='h', bar_color=('white', 'red')):
31+
print('Hit the break')
32+
break
33+
for i in range(10000):
34+
if not sg.OneLineProgressMeter('My 1-line progress meter', i+1, 10000, 'meter key', 'MY MESSAGE1', 'MY MESSAGE 2',orientation='v' ):
35+
print('Hit the break')
3136
break
32-
3337

3438
layout = [
3539
[sg.T('One-Line Progress Meter Demo', font=('Any 18'))],
@@ -60,30 +64,74 @@ def DemoOneLineProgressMeter():
6064
break
6165
sleep(delay_inner/1000)
6266

67+
68+
'''
69+
Manually Updated Test
70+
Here is an example for when you want to "sprinkle" progress bar updates in multiple
71+
places within your source code and you're not running an event loop.
72+
Note that UpdateBar is special compared to other Update methods. It also refreshes
73+
the containing window and checks for window closure events
74+
The sleep calls are here only for demonstration purposes. You should NOT be adding
75+
these kinds of sleeps to a GUI based program normally.
76+
'''
77+
78+
def manually_updated_meter_test():
79+
# layout the form
80+
layout = [[sg.Text('This meter is manually updated 4 times')],
81+
[sg.ProgressBar(10, orientation='h', size=(20,20), key='progress')]]
82+
83+
# create the form`
84+
window = sg.Window('Custom Progress Meter', layout).Finalize()
85+
progress_bar = window.FindElement('progress')
86+
87+
# -------------------- Your Program Code --------------------
88+
# Spot #1 to indicate progress
89+
progress_bar.UpdateBar(1) # show 10% complete
90+
sleep(2)
91+
92+
# more of your code.... perhaps pages and pages of code.
93+
# Spot #2 to indicate progress
94+
progress_bar.UpdateBar(2) # show 20% complete
95+
sleep(2)
96+
97+
# more of your code.... perhaps pages and pages of code.
98+
# Spot #3 to indicate progress
99+
progress_bar.UpdateBar(6) # show 60% complete
100+
sleep(2)
101+
102+
# more of your code.... perhaps pages and pages of code.
103+
# Spot #4 to indicate progress
104+
progress_bar.UpdateBar(9) # show 90% complete
105+
sleep(2)
106+
window.Close()
107+
108+
63109
'''
64-
Make your own progress meter!
65-
Embed the meter right into your window
110+
This function shows how to create a custom window with a custom progress bar and then
111+
how to update the bar to indicate progress is being made
66112
'''
67113

68-
def CustomMeter():
114+
def custom_meter_example():
69115
# layout the form
70-
layout = [[sg.Text('A custom progress meter')],
71-
[sg.ProgressBar(1000, orientation='h', size=(20,20), key='progress')],
116+
layout = [[sg.Text('A typical custom progress meter')],
117+
[sg.ProgressBar(1, orientation='h', size=(20,20), key='progress')],
72118
[sg.Cancel()]]
73119

74120
# create the form`
75121
window = sg.Window('Custom Progress Meter').Layout(layout)
76122
progress_bar = window.FindElement('progress')
77123
# loop that would normally do something useful
78-
for i in range(1000):
124+
for i in range(10000):
79125
# check to see if the cancel button was clicked and exit loop if clicked
80126
event, values = window.Read(timeout=0)
81127
if event == 'Cancel' or event == None:
82128
break
83129
# update bar with loop value +1 so that bar eventually reaches the maximum
84-
progress_bar.UpdateBar(i+1)
130+
progress_bar.UpdateBar(i+1, 10000)
85131
# done with loop... need to destroy the window as it's still open
86132
window.Close()
87133

88-
CustomMeter()
89-
DemoOneLineProgressMeter()
134+
135+
manually_updated_meter_test()
136+
custom_meter_example()
137+
demo_one_line_progress_meter()

0 commit comments

Comments
 (0)