【发布时间】:2020-07-09 02:43:03
【问题描述】:
我需要向使用 PySimpleGUI 构建的控制面板添加“添加新控件组”功能。每次选择哪种控件组,因此无法像this answer中描述的那样预定义如何在PySimple GUI中基于按钮点击显示不同的布局? (持续窗口循环)
为简洁起见,我将这里的问题一直抽象为一个按钮,每次按下它时都应该添加另一个滑块。
我尝试使用window.Layout 进行更新,但由于它重用现有对象而被拒绝。我尝试了copy.deepcopy,但也失败了。
有没有办法通过从选项列表中选择来动态添加一组新的控件(多次)?
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)
错误信息:
# UserWarning: *** YOU ARE ATTEMPTING TO RESUSE AN ELEMENT IN YOUR LAYOUT!
Once placed in a layout, an element cannot be used in another layout. ***`
【问题讨论】:
标签: python python-3.x user-interface widget pysimplegui