Raj
Raj

Reputation: 43

Update not changing visibility in PySimpleGUI

I am trying to make a GUI element invisible by using visible=False. It appears that the update function works with every other attribute as well as for values s shown in the code below, except for the visible attribute. Specifically, although the program requests that element (2,2) become invisible, element (4,2) is becoming invisible. Any help with this will be greatly appreciated. Thanks

import PySimpleGUI as sg

layout = [[sg.B(' ', size=(8,4), key=(i,j)) for i in range(0,5)] for j in range(0,4)]

window = sg.Window('Trying to change attribute', layout).Finalize()
window.Maximize()

while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    window[(1,1)].update(button_color=('blue','yellow'))
    window[(2,2)].update(visible=False)  ## Problem here
    window[(3,3)].update('Hello')

    if event in (None, 'Exit'):
        break
    current_marker = window[event].get_text()

Upvotes: 2

Views: 4143

Answers (2)

The behaviour that you are experiencing is due to the fact that the elements that are made invisible loose their position in the layout. In other words, element (2,2) disappears. If you'd make it visible later, it will appear at the end of its respective row.

In most cases, this is not the behaviour we'd want. You can prevent it by reserving a place for the element using a function like place() shown below:

import PySimpleGUI as sg


def place(elem, size=(None, None)):
    return sg.Column([[elem]], size=size, pad=(0, 0), element_justification='center')


def test():
    layout = [[place(sg.B(' ', size=(8, 4), key=(i, j))) for i in range(0, 5)] for j in range(0, 4)]

    window = sg.Window('Trying to change attribute', layout).Finalize()
    window.Maximize()

    while True:  # Event Loop
        event, values = window.read()
        if event in (None, 'Exit'):
            break
        print(event, values)
        window[(1, 1)].update(button_color=('blue', 'yellow'))
        window[(2, 2)].update(visible=False)
        window[(3, 3)].update('Hello')

This is the result:

enter image description here

Upvotes: 1

Bhargav Desai
Bhargav Desai

Reputation: 1016

The code working properly.

befor click

when you click any button. it update visible=False and it remove from the screen. so button (3,2) and (4,2) shift left side. so , we think that (4,2) is invisible instead of (2,2).

enter image description here

you may also check with disabling button.

window[(2,2)].update(disabled=True)

enter image description here

Upvotes: 0

Related Questions