Created a custoim progress meter example

This commit is contained in:
MikeTheWatchGuy 2018-09-28 19:21:44 -04:00
parent 6cc8ed1ad1
commit 509c8fd7ec
2 changed files with 59 additions and 55 deletions

View File

@ -35,15 +35,8 @@ def draw_figure(canvas, figure, loc=(0, 0)):
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
figure_w, figure_h = int(figure_w), int(figure_h)
photo = Tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
# Position: convert from top-left anchor to center anchor
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
# Unfortunately, there's no accessor for the pointer to the native renderer
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
# Return a handle which contains a reference to the photo object
# which must be kept live or else the picture disappears
return photo
#------------------------------- PASTE YOUR MATPLOTLIB CODE HERE -------------------------------
@ -94,25 +87,14 @@ plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)
# Format the minor tick labels of the y-axis into empty strings with
# `NullFormatter`, to avoid cumbering the axis with too many labels.
plt.gca().yaxis.set_minor_formatter(NullFormatter())
# Adjust the subplot layout, because the logit one may take more space
# than usual, due to y-tick labels like "1 - 10^{-3}"
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
wspace=0.35)
fig = plt.gcf() # if using Pyplot then get the figure from the plot
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
#------------------------------- END OF YOUR MATPLOTLIB CODE -------------------------------
# ****** Comment out this line if not using Pyplot ******
fig = plt.gcf() # if using Pyplot then get the figure from the plot
# -------------------------------- GUI Starts Here -------------------------------#
# fig = your figure you want to display. Assumption is that 'fig' holds the #
# information to display. #
# --------------------------------------------------------------------------------#
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
# define the form layout
layout = [[sg.Text('Plot test', font='Any 18')],
[sg.Canvas(size=(figure_w, figure_h), key='canvas')],

View File

@ -24,18 +24,14 @@ from sys import exit as exit
The simple case is that you want to add a single meter to your code. The one-line solution
"""
# Display a progress meter in work loop. User is not allowed to break out of the loop
for i in range(10000):
if i % 5 == 0: sg.OneLineProgressMeter('My 1-line progress meter', i+1, 10000, 'single')
# Display a progress meter. Allow user to break out of loop using cancel button
for i in range(10000):
if not sg.OneLineProgressMeter('My 1-line progress meter', i+1, 10000, 'single'):
def DemoOneLineProgressMeter():
# Display a progress meter. Allow user to break out of loop using cancel button
for i in range(1000):
if not sg.OneLineProgressMeter('My 1-line progress meter', i+1, 1000, 'meter key' ):
break
layout = [
layout = [
[sg.T('One-Line Progress Meter Demo', font=('Any 18'))],
[sg.T('Outer Loop Count', size=(15,1), justification='r'), sg.In(default_text='100', size=(5,1), key='CountOuter', do_not_clear=True),
sg.T('Delay'), sg.In(default_text='10', key='TimeOuter', size=(5,1), do_not_clear=True), sg.T('ms')],
@ -44,9 +40,9 @@ layout = [
[sg.Button('Show', pad=((0,0), 3), bind_return_key=True), sg.T('me the meters!')]
]
window = sg.Window('One-Line Progress Meter Demo').Layout(layout)
window = sg.Window('One-Line Progress Meter Demo').Layout(layout)
while True:
while True:
button, values = window.Read()
if button is None:
break
@ -64,4 +60,30 @@ while True:
break
sleep(delay_inner/1000)
exit(69)
'''
Make your own progress meter!
Embed the meter right into your window
'''
def CustomMeter():
# layout the form
layout = [[sg.Text('A custom progress meter')],
[sg.ProgressBar(10000, orientation='h', size=(20,20), key='progress')],
[sg.Cancel()]]
# create the form`
window = sg.Window('Custom Progress Meter').Layout(layout)
progress_bar = window.FindElement('progress')
# loop that would normally do something useful
for i in range(10000):
# check to see if the cancel button was clicked and exit loop if clicked
button, values = window.ReadNonBlocking()
if button == 'Cancel' or values == None:
break
# update bar with loop value +1 so that bar eventually reaches the maximum
progress_bar.UpdateBar(i+1)
# done with loop... need to destroy the window as it's still open
window.CloseNonBlocking()
CustomMeter()
DemoOneLineProgressMeter()