From 5722f1ab099ae265501f2fd7b042e7a04d43f71c Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 11 Nov 2022 10:35:56 -0500 Subject: [PATCH] New Demo showing how to redraw figures on a Graph if the window resizes --- DemoPrograms/Demo_Graph_Window_Resize.py | 45 ++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 DemoPrograms/Demo_Graph_Window_Resize.py diff --git a/DemoPrograms/Demo_Graph_Window_Resize.py b/DemoPrograms/Demo_Graph_Window_Resize.py new file mode 100644 index 00000000..b571c774 --- /dev/null +++ b/DemoPrograms/Demo_Graph_Window_Resize.py @@ -0,0 +1,45 @@ +import PySimpleGUI as sg + +""" + Demo - Graph Element Rescale Figures When Window Resizes + + This demo shows how you can redraw your Graph element's figures so that when + you resize the window, all of the figures on the graph resize. + + There may be a tkinter method to help do this? + + Copyright 2022 PySimpleGUI +""" + +gsize = (400,400) + +layout = [ [sg.Text('Rescaling a Graph Element When Window is Resized')], + [sg.Graph(gsize, (0,0),gsize, expand_x=True, expand_y=True, k='-G-', background_color='green')], + [sg.Button('Exit'), sg.Sizegrip()] ] + +window = sg.Window('Graph Element Scale With Window', layout, finalize=True, resizable=True, enable_window_config_events=True) + +graph = window['-G-'] #type: sg.Graph + +orig_win_size = window.current_size_accurate() +# Draw the figure desired (will repeat this code later) +fig = window['-G-'].draw_circle((200, 200), 50, fill_color='blue') + +while True: + event, values = window.read() + if event == sg.WIN_CLOSED or event == 'Exit': + break + if event == sg.WINDOW_CONFIG_EVENT: # if get a window resized event + # Determine how much the window was resized by and tell the Graph element the new size for the Canvas + new_size = window.current_size_accurate() + dx = orig_win_size[0]-new_size[0] + dy = orig_win_size[1]-new_size[1] + gsize = (gsize[0] - dx, gsize[1] - dy) + orig_win_size = new_size + graph.CanvasSize = gsize + # Erase entire Graph and redraw all figures0 + graph.erase() + # Redraw your figures here + fig = window['-G-'].draw_circle((200, 200), 50, fill_color='blue') + +window.close()