(Explanation of my problem at the end)
Here is an example of a dynamic layout that add a Frame when I click on a button. Launch state of the program :
Then after button pressed :
Here is the code :
import PySimpleGUI as sg
def layout_for_frame(lvl_1):
layout = [[sg.Frame(title = 'Frame level 2',
layout = [[sg.Text('Text 2 '), sg.Input(key=('-level two-',lvl_1))],
[sg.Combo(['A', 'B'],key=('-combo-',lvl_1))]],
relief = 'sunken')]]
return layout
def make_window():
layout = [[sg.Frame(title = 'Frame level 1',
layout = layout_for_frame(0),
key = '-Frame lvl 1-',
relief = 'groove')],
[sg.Button('+',key = 'add a level two Frame')]]
window = sg.Window('Window Title', layout,metadata=0)
return window
def main():
window = make_window()
while True:
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED :
break
elif event == 'add a level two Frame' :
window.metadata += 1
window.extend_layout(window['-Frame lvl 1-'],(layout_for_frame(window.metadata)))
window.close()
if __name__ == '__main__':
main()
This works perfectly for adding a frame inside another one. The problem is adding another Frame level 1. Because I don't know how to call a function with a key without an element in the layout :
layout = [[(layout_for_frame(0),key = '-CALL FOR FRAME-')],
[sg.Button('+',key = 'add a level one Frame')]]
This does not work, and so I can't use the same logic shown before.
My question is how to add directly a level 1 frame with a button ?
Please let me know if it is not clear, and thank you for taking the time to read through it.


