Reputation: 3745
I need an "add new group of controls" feature to a control panel built with PySimpleGUI. One selects which kind of group of controls each time, so it can't be predefined similarly to what is described in this answer to How to display different layouts based on button clicks in PySimple GUI? (Persistent window loop)
For brevity I've abstracted the problem here all the way down to a button that should add another slider each time it is pushed.
I've tried to update using window.Layout
but this is rejected because it reuses existing objects. I tried a copy.deepcopy
but that fails as well.
Is there a way to dynamically add a new group of controls (multiple times) specified by selecting from a list of options?
import PySimpleGUI as sg
s = {'range': (2,6), 'resolution': 0.2, 'default_value': 5,
'size': (20,15), 'orientation': 'horizontal',
'font': ('Helvetica', 8), 'enable_events': True}
layout = [[sg.Slider(**s, key='hi'), sg.Button('Add Slider')]]
window = sg.Window('wow!', layout=layout, background_color="gray",
size=(400, 200))
while True:
event, values = window.read()
if event is None:
break
print(event, values)
if event == 'Add Slider':
layout[0][0].Update(value=8.0 - values['hi'])
layout.append(sg.Slider(**s))
window.layout(layout)
Error message:
# UserWarning: *** YOU ARE ATTEMPTING TO RESUSE AN ELEMENT IN YOUR LAYOUT!
Once placed in a layout, an element cannot be used in another layout. ***`
Upvotes: 0
Views: 1597
Reputation: 2533
Could you try using a function?
def Btn():
return sg.Slider(**s)
layout.append(Btn())
Upvotes: 1