I have a GUI in Python using PySimpleGUI with a Graph element (drawing canvas) on it. I want the user to be able to resize the window. The drawing (Graph element) should resize along with the window (as well as all the drawing elements on the Graph element).
I tried the following code, but the resizing doesn't work. Either the circle remains the same size in pixels, regardless of the window size (current code) or the circle does resize, but resizing the Graph element triggers a Window resize event by itself (uncomment line 13 with the graph.set_size() method)
Question: how to resize the Graph element together with the Window, and also resize the elements on the Graph canvas. In the example code: I want the circle to resize so that it fits inside the window.
import PySimpleGUI as sg
INIT_SIZE_PX = (100, 100)
COORD_SYSTEM = [(0, 0), (2, 2)]
CIRCLE_CENTER = (1, 1)
CIRCLE_RADIUS = 1
CIRCLE_COLOR = 'red'
def draw_graph(graph, size):
graph.erase()
# graph.set_size(size) # NOT the way, triggers resize events on its own
# graph.change_coordinates(*COORD_SYSTEM) # Not necessary
graph.draw_circle(CIRCLE_CENTER, CIRCLE_RADIUS, fill_color=CIRCLE_COLOR)
layout = [[
sg.Graph(INIT_SIZE_PX, *COORD_SYSTEM, key='graph',
expand_x=True, expand_y=True)
]]
window = sg.Window('Window title', layout=layout,
finalize=True, resizable=True)
window.bind('<Configure>', "Resize_Event")
draw_graph(window['graph'], window.size)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
elif event == "Resize_Event":
new_size = (int(window.size[0] * 0.9), int(window.size[1] * 0.9))
draw_graph(window['graph'], new_size)
print(f"Resized to: {window.size=}; {window['graph'].Size=}")
window.close()

