4

How can I get notified when the window is resized in PySimpleGUI?

I have a window that enables resize events, but I'm not finding a way to move the elements around when that resize occurs, so my window renames top left centered the same size when the window changes size.

Here is the basic code:

import PySimpleGUI as sg

layout = [[sg.Button('Save')]]
window = sg.Window('Window Title', 
                   layout,
                   default_element_size=(12, 1),
                   resizable=True)  # this is the change

while True:
    event, values = window.read()
    if event == 'Save':
        print('clicked save')

    if event == sg.WIN_MAXIMIZED:  # I just made this up, and it does not work. :)
        window.maximize()

    if event == sg.WIN_CLOSED:
        break
nycynik
  • 7,371
  • 8
  • 62
  • 87

2 Answers2

7

Adding tkinter events to windows results in callback on change of windows size

import PySimpleGUI as sg


layout = [[sg.Button('Save')]]
window = sg.Window('Window Title',
                   layout,
                   default_element_size=(12, 1),
                   resizable=True,finalize=True)  # this is the chang
window.bind('<Configure>',"Event")

while True:
    event, values = window.read()
    if event == 'Save':
        print('clicked save')

    if event == "Event":
        print(window.size)

    if event == sg.WIN_CLOSED:
        print("I am done")
        break
raghu
  • 335
  • 4
  • 11
  • 2
    +1 for mentioning window.size. It could be used to create a non tkinter specific solution (with window.read(timeout=100) and checking the size for changes) – rtrrtr Apr 13 '21 at 21:35
3

You need to bind "<Configure>" event to check zoomed event.

import PySimpleGUI as sg

layout = [[sg.Text('Window normal', size=(30, 1), key='Status')]]
window = sg.Window('Title', layout, resizable=True, finalize=True)
window.bind('<Configure>', "Configure")
status = window['Status']

while True:

    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'Configure':
        if window.TKroot.state() == 'zoomed':
            status.update(value='Window zoomed and maximized !')
        else:
            status.update(value='Window normal')

window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23