ClearCode

Graph

Apr 1st, 2022
2,397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. import PySimpleGUI as sg
  2. import matplotlib
  3. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  4.  
  5. def update_figure(data):
  6.     axes = fig.axes
  7.     x = [i[0] for i in data]
  8.     y = [int(i[1]) for i in data]
  9.     axes[0].plot(x,y,'r-')
  10.     figure_canvas_agg.draw()
  11.     figure_canvas_agg.get_tk_widget().pack()
  12.  
  13. sg.theme('DarkTeal6')
  14. table_content = []
  15. layout = [
  16.     [sg.Table(
  17.         headings = ['Observation','Result'],
  18.         values = table_content,
  19.         expand_x = True,
  20.         hide_vertical_scroll = True,
  21.         key = '-TABLE-')],
  22.     [sg.Input(key = '-INPUT-',expand_x = True),sg.Button('Submit')],
  23.     [sg.Canvas(key = '-CANVAS-')]
  24. ]
  25.  
  26. window = sg.Window('Graph App', layout, finalize = True)
  27.  
  28. # matplotlib
  29. fig = matplotlib.figure.Figure(figsize = (5,4))
  30. fig.add_subplot(111).plot([],[])
  31. figure_canvas_agg = FigureCanvasTkAgg(fig,window['-CANVAS-'].TKCanvas)
  32. figure_canvas_agg.draw()
  33. figure_canvas_agg.get_tk_widget().pack()
  34.  
  35.  
  36. while True:
  37.     event, values = window.read()
  38.     if event == sg.WIN_CLOSED:
  39.         break
  40.  
  41.     if event == 'Submit':
  42.         new_value = values['-INPUT-']
  43.         if new_value.isnumeric():
  44.             table_content.append([len(table_content) + 1,float(new_value)])
  45.             window['-TABLE-'].update(table_content)
  46.             window['-INPUT-'].update('')
  47.             update_figure(table_content)
  48.  
  49. window.close()
Advertisement
Add Comment
Please, Sign In to add comment