hide_row() in pysimplegui work only one time?

Viewed 128

I use hide_row() element or feature in my code to hide row but it work only one time when i connected to Retreat button or Retreat key, when i clicked again or in the 2nd time it's not work (see the code or try it to understand what i mean) how make it work continuously not only one time or is there another way to undo or delete section or row.

import PySimpleGUI as sg

layout = [

[sg.Col([[sg.T('Enter your name:'),sg.In()],[sg.Btn('Ok'), sg.Btn('Exit'),
sg.Btn('Add new row')]],k='col_key')]

]

window = sg.Window('Test', layout)

while True:
    event, values = window.read()
    if event in ('Exit' ,sg.WIN_CLOSED):
        break

    if event == ('Add new row'):
        window.extend_layout(window['col_key'],[[sg.T('Enter your name:', key='new_raw'),(sg.In())]])
        window.extend_layout(window['col_key'],[[sg.Btn('Retreat', key='re')]])

    if event == ('re'):
        window['new_raw'].hide_row()
        window['re'].hide_row()

window.close()
1 Answers

It is caused by new element with duplicate key when add new row again, so it wll always find the first element when you call window[new_key].

Should use different key for different element when you create a new element.

Key of new element will be replace by

element.Key = str(element.Key) + str(self.UniqueKeyCounter)

So key for new elements will be

'new_raw', 're', 'new_raw0', 're1', 'new_raw2', 're3',  'new_raw4', 're5', ...

you can check it by

>>> window.key_dict
{
...
 'new_raw': <PySimpleGUI.PySimpleGUI.Text object at 0x000001D3A5B80FA0>,
 'new_raw0': <PySimpleGUI.PySimpleGUI.Text object at 0x000001D3A6FA1280>,
 'new_raw2': <PySimpleGUI.PySimpleGUI.Text object at 0x000001D3A6FA1220>,
 're': <PySimpleGUI.PySimpleGUI.Button object at 0x000001D3A6FA1CD0>,
 're1': <PySimpleGUI.PySimpleGUI.Button object at 0x000001D3A6FA1340>,
 're3': <PySimpleGUI.PySimpleGUI.Button object at 0x000001D3A6FA11F0>}

After new row generated, the key of button Retreat is not 're', but starts with 're' and ends with an index.

Update code as following

import PySimpleGUI as sg

layout = [

[sg.Col([[sg.T('Enter your name:'),sg.In()],[sg.Btn('Ok'), sg.Btn('Exit'),
sg.Btn('Add new row')]],k='col_key')]

]

window = sg.Window('Test', layout)
index = 0
while True:
    event, values = window.read()
    if event in ('Exit' ,sg.WIN_CLOSED):
        break

    if event == ('Add new row'):
        window.extend_layout(window['col_key'],[[sg.T('Enter your name:', key=f'new_raw {index}'),(sg.In(key=f'input {index}'))]])
        window.extend_layout(window['col_key'],[[sg.Btn('Retreat', key=f're {index}')]])
        index += 1

    if event.startswith('re'):
        i = event.split(' ')[-1]
        window[f'new_raw {i}'].hide_row()
        window[f're {i}'].hide_row()

window.close()

There's still problem for old rows hidden will still be kept in memory, so occupied memory will grow higher and higher if you add new row again and again, but it's not question to be discussed here.

Related