Pysimplegui hide element

pysimplegui hide element

With PySimpleGUI, you can hide an element by setting its visible property to False.

Here’s an example of how to hide a button:

import PySimpleGUI as sg

layout = [
    [sg.Button('Button', visible=False)],
    [sg.Button('Show Button', key='-SHOW-')]
]

window = sg.Window('Hide Element', layout)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == '-SHOW-':
        window['Button'].update(visible=True)

window.close()
    

In this example, we create a window with a button initially set to be invisible by setting the visible property of the button element to False. We also include a second button that triggers the visibility of the first button by updating its visible property to True.

Note that when an element is hidden, it still takes up space in the layout, but it is not visible to the user. To completely remove an element from the layout, you can use the window.Element() method to reference the element and then use the widget.Unpack() method to remove it.

Here’s an example:

import PySimpleGUI as sg

layout = [
    [sg.Button('Button')],
    [sg.Button('Remove Button', key='-REMOVE-')]
]

window = sg.Window('Hide Element', layout)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == '-REMOVE-':
        button = window.Element('Button')
        button.Unpack()

window.close()
    

In this example, we create a window with a button and another button that triggers the removal of the first button from the layout. The window.Element() method is used to reference the button element, and the widget.Unpack() method is used to remove it from the layout.

By hiding or removing elements dynamically, you can create interactive and responsive GUI applications using PySimpleGUI.

Leave a comment