【问题标题】:PySimpleGui can not access list in InputComboPySimpleGui 无法访问 InputCombo 中的列表
【发布时间】: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.InputComboevent=="add" 是错误的,应该是 event=="-add-" 因为你将按钮 add 的键设置为'-add-'。
  • 感谢您的回答。但我不明白。我对 PySImpleGui 比较陌生。我已将add 更改为-add-
  • 已回复。

标签: python user-interface pysimplegui


【解决方案1】:

调用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()

【讨论】:

    猜你喜欢
    • 2015-07-14
    • 2015-02-19
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    相关资源
    最近更新 更多