diff --git a/Demo_Matplotlib.py b/Demo_Matplotlib.py index 32e47960..8cf6eb01 100644 --- a/Demo_Matplotlib.py +++ b/Demo_Matplotlib.py @@ -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')], diff --git a/Demo_Progress_Meters.py b/Demo_Progress_Meters.py index 8386a9d7..0fd70e24 100644 --- a/Demo_Progress_Meters.py +++ b/Demo_Progress_Meters.py @@ -24,44 +24,66 @@ 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'): - break +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 = [ - [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')], - [sg.T('Inner Loop Count', size=(15,1), justification='r'), sg.In(default_text='100', size=(5,1), key='CountInner', do_not_clear=True) , - sg.T('Delay'), sg.In(default_text='10', key='TimeInner', size=(5,1), do_not_clear=True), sg.T('ms')], - [sg.Button('Show', pad=((0,0), 3), bind_return_key=True), sg.T('me the meters!')] - ] + 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')], + [sg.T('Inner Loop Count', size=(15,1), justification='r'), sg.In(default_text='100', size=(5,1), key='CountInner', do_not_clear=True) , + sg.T('Delay'), sg.In(default_text='10', key='TimeInner', size=(5,1), do_not_clear=True), sg.T('ms')], + [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: - button, values = window.Read() - if button is None: - break - if button == 'Show': - max_outer = int(values['CountOuter']) - max_inner = int(values['CountInner']) - delay_inner = int(values['TimeInner']) - delay_outer = int(values['TimeOuter']) - for i in range(max_outer): - if not sg.OneLineProgressMeter('Outer Loop', i+1, max_outer, 'outer'): - break - sleep(delay_outer/1000) - for j in range(max_inner): - if not sg.OneLineProgressMeter('Inner Loop', j+1, max_inner, 'inner'): + while True: + button, values = window.Read() + if button is None: + break + if button == 'Show': + max_outer = int(values['CountOuter']) + max_inner = int(values['CountInner']) + delay_inner = int(values['TimeInner']) + delay_outer = int(values['TimeOuter']) + for i in range(max_outer): + if not sg.OneLineProgressMeter('Outer Loop', i+1, max_outer, 'outer'): break - sleep(delay_inner/1000) + sleep(delay_outer/1000) + for j in range(max_inner): + if not sg.OneLineProgressMeter('Inner Loop', j+1, max_inner, 'inner'): + 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() \ No newline at end of file