PySimpleGui无法访问InputCombo中的列表

2024-04-30 04:34:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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-"])

我有一张“东西”的清单。如果在inputfield中写入内容,我可以添加一个新值。通过“添加”按钮,我将值添加到我的“物品”列表中。通过InputCombo,我可以访问列表中的V值,例如“a”和“b”。如果我选择“a”或“b”并按“写入”,文本字段将更新并写入“a”或“b”。但是在InputCombo中,我不能选择值,这是我后来添加的。 有人知道我怎样才能把事情做好吗


Tags: key脚本eventadd列表inputifbutton
1条回答
网友
1楼 · 发布于 2024-04-30 04:34:22

调用sg.InputCombo的方法update(values=things)来更新新值

修订守则

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
    elif event == "-add-":                          # Correct event to add
        things.append(values["-input-"])
        window['-combo-'].update(values=things)     # Statement to update
    elif event == "write":
        window["-output-"].update(values["-combo-"])

window.close()

相关问题 更多 >