【发布时间】:2021-08-21 07:35:42
【问题描述】:
我正在使用 PySimpleGui 为我的 python 脚本制作一个简单的 gui,并输入了一个问题。每次我想向 InputCombo 列表添加一个新整数时,我都无法访问新整数。
我写了一个基本的脚本来展示:
import PySimpleGUI as sg
things=["a","b"]
layout=[[sg.Input(key="-input-",size=(10,1)),sg.Button("add",key="-add-")],
[sg.InputCombo(things,key="-combo-"),sg.Button("write")],
[sg.Text("",key="-output-"),sg.Button("Quit")]]
window=sg.Window("Name",layout,size=(200,200))
while True:
event,values=window.read()
if event==sg.WINDOW_CLOSED or event=="Quit":
break
if event=="add":
things.append(values["-input"])
if event=="write":
window["-output-"].update(values["-combo-"])
我有一个“事物”列表。如果我在输入字段中写一些东西,我可以添加一个新值。使用“添加”按钮,我将值添加到我的列表“事物”中。使用 InputCombo,我可以访问列表中的 vvalue,例如“a”和“b”。如果我选择“a”或“b”并按下“write”,文本字段将更新并写入“a”或“b”。但是在 InputCombo 中我不能选择值,这是我稍后添加的。 有人知道我怎样才能让事情正常进行吗?
【问题讨论】:
-
按钮
add只是将新项目添加到列表things,而不是添加到sg.InputCombo。您需要使用选项values=things更新sg.InputCombo。event=="add"是错误的,应该是event=="-add-"因为你将按钮add的键设置为'-add-'。 -
感谢您的回答。但我不明白。我对 PySImpleGui 比较陌生。我已将
add更改为-add-。 -
已回复。
标签: python user-interface pysimplegui