Skip to content
Advertisement

How do I respond to window resize in PySimpleGUI

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

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement