如何在 PySimpleGUI 中响应 window resize

How do I respond to window resize in PySimpleGUI

当 window 在 PySimpleGUI 中调整大小时,我如何得到通知?

我有一个 window 可以启用调整大小事件,但是我找不到在发生调整大小时移动元素的方法,所以我的 window 将左上角重命名为相同大小当 window 改变大小时。

基本代码如下:

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

将 tkinter 事件添加到 windows 导致在 windows 大小更改时回调

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

您需要绑定 "<Configure>" 事件以检查缩放事件。

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()