def main(): global g_my_globals # define the form layout layout = [[ sg.Canvas(size=SIZE, background_color='white', key='canvas'), sg.ReadButton('Exit', pad=(0, (210, 0))) ]] # create the form and show it without the plot window = sg.Window('Ping Graph', background_color='white', grab_anywhere=True).Layout(layout).Finalize() canvas_elem = window.FindElement('canvas') canvas = canvas_elem.TKCanvas fig = plt.figure(figsize=(3.1, 2.25), tight_layout={'pad': 0}) g_my_globals.axis_ping = fig.add_subplot(1, 1, 1) plt.rcParams['xtick.labelsize'] = 8 plt.rcParams['ytick.labelsize'] = 8 set_chart_labels() plt.tight_layout() while True: button, values = window.ReadNonBlocking() if button is 'Exit' or values is None: exit(0) run_a_ping_and_graph() photo = draw(fig, canvas)
def pong(): # ------------- Define GUI layout ------------- layout = [[ sg.Canvas(size=(700, 400), background_color='black', key='canvas') ], [sg.T(''), sg.ReadButton('Quit')]] # ------------- Create window ------------- window = sg.Window('The Classic Game of Pong', return_keyboard_events=True).Layout(layout).Finalize() # window.Finalize() # TODO Replace with call to window.Finalize once code released # ------------- Get the tkinter Canvas we're drawing on ------------- canvas = window.FindElement('canvas').TKCanvas # ------------- Create line down center, the bats and ball ------------- canvas.create_line(350, 0, 350, 400, fill='white') bat1 = pongbat(canvas, 'white') bat2 = pongbat2(canvas, 'white') ball1 = Ball(canvas, bat1, bat2, 'green') # ------------- Event Loop ------------- while True: # ------------- Draw ball and bats ------------- ball1.draw() bat1.draw() bat2.draw() # ------------- Read the form, get keypresses ------------- button, values = window.ReadNonBlocking() # ------------- If quit ------------- if button is None and values is None or button == 'Quit': exit(69) # ------------- Keypresses ------------- if button is not None: if button.startswith('Up'): bat2.up(2) elif button.startswith('Down'): bat2.down(2) elif button == 'w': bat1.up(1) elif button == 's': bat1.down(1) if ball1.checkwin(): sg.Popup('Game Over', ball1.checkwin() + ' won!!') break # ------------- Bottom of loop, delay between animations ------------- # time.sleep(.01) canvas.after(10)
def main(): global g_my_globals # define the form layout layout = [[ sg.Text('Animated Ping', size=(40, 1), justification='center', font='Helvetica 20') ], [sg.Canvas(size=(640, 480), key='canvas')], [ sg.ReadButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14') ]] # create the form and show it without the plot window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI' ).Layout(layout).Finalize() canvas_elem = window.FindElement('canvas') canvas = canvas_elem.TKCanvas fig = plt.figure() g_my_globals.axis_ping = fig.add_subplot(1, 1, 1) set_chart_labels() plt.tight_layout() while True: button, values = window.ReadNonBlocking() if button is 'Exit' or values is None: break run_a_ping_and_graph() photo = draw(fig, canvas)
sg.ChangeLookAndFeel('LightGreen') figure_w, figure_h = 650, 650 # define the form layout listbox_values = [key for key in fig_dict.keys()] col_listbox = [[ sg.Listbox(values=listbox_values, change_submits=True, size=(28, len(listbox_values)), key='func') ], [sg.T(' ' * 12), sg.Exit(size=(5, 2))]] layout = [ [sg.Text('Matplotlib Plot Test', font=('current 18'))], [ sg.Column(col_listbox, pad=(5, (3, 330))), sg.Canvas(size=(figure_w, figure_h), key='canvas'), sg.Multiline(size=(70, 35), pad=(5, (3, 90)), key='multiline') ], ] # create the form and show it without the plot window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', grab_anywhere=False).Layout(layout) window.Finalize() canvas_elem = window.FindElement('canvas') multiline_elem = window.FindElement('multiline') while True: button, values = window.Read() print(button)
def main(): fig = Figure() ax = fig.add_subplot(111) ax.set_xlabel("X axis") ax.set_ylabel("Y axis") ax.grid() # define the form layout layout = [[ sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20') ], [sg.Canvas(size=(640, 480), key='canvas')], [ sg.Slider(range=(0, 10000), size=(60, 10), orientation='h', key='slider') ], [ sg.ReadButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14') ]] # create the form and show it without the plot window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI' ).Layout(layout).Finalize() canvas_elem = window.FindElement('canvas') slider_elem = window.FindElement('slider') graph = FigureCanvasTkAgg(fig, master=canvas_elem.TKCanvas) canvas = canvas_elem.TKCanvas dpts = [randint(0, 10) for x in range(10000)] for i in range(len(dpts)): button, values = window.ReadNonBlocking() if button is 'Exit' or values is None: exit(69) slider_elem.Update(i) ax.cla() ax.grid() DATA_POINTS_PER_SCREEN = 40 ax.plot(range(DATA_POINTS_PER_SCREEN), dpts[i:i + DATA_POINTS_PER_SCREEN], color='purple') graph.draw() figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds figure_w, figure_h = int(figure_w), int(figure_h) photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h) canvas.create_image(640 / 2, 480 / 2, image=photo) figure_canvas_agg = FigureCanvasAgg(fig) figure_canvas_agg.draw() # Unfortunately, there's no accessor for the pointer to the native renderer tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
def main(): # define the form layout layout = [[ sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20') ], [sg.Canvas(size=(640, 480), key='canvas')], [ sg.ReadButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14') ]] # create the form and show it without the plot window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI' ).Layout(layout).Finalize() canvas_elem = window.FindElement('canvas') canvas = canvas_elem.TKCanvas while True: button, values = window.ReadNonBlocking() if button is 'Exit' or values is None: exit(69) def PyplotScatterWithLegend(): import matplotlib.pyplot as plt from numpy.random import rand fig, ax = plt.subplots() for color in ['red', 'green', 'blue']: n = 750 x, y = rand(2, n) scale = 200.0 * rand(n) ax.scatter(x, y, c=color, s=scale, label=color, alpha=0.3, edgecolors='none') ax.legend() ax.grid(True) return fig fig = PyplotScatterWithLegend() figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds figure_w, figure_h = int(figure_w), int(figure_h) photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h) canvas.create_image(640 / 2, 480 / 2, image=photo) figure_canvas_agg = FigureCanvasAgg(fig) figure_canvas_agg.draw() # Unfortunately, there's no accessor for the pointer to the native renderer tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
#!/usr/bin/env python import sys if sys.version_info[0] < 3: import PySimpleGUI27 as sg else: import PySimpleGUI as sg layout = [[sg.Canvas(size=(150, 150), background_color='red', key='canvas')], [ sg.T('Change circle color to:'), sg.ReadButton('Red'), sg.ReadButton('Blue') ]] window = sg.Window('Canvas test').Layout(layout).Finalize() cir = window.FindElement('canvas').TKCanvas.create_oval(50, 50, 100, 100) while True: button, values = window.Read() if button is None: break if button is 'Blue': window.FindElement('canvas').TKCanvas.itemconfig(cir, fill="Blue") elif button is 'Red': window.FindElement('canvas').TKCanvas.itemconfig(cir, fill="Red")