英文:
I get "TypeError: list indices must be integers or slices, not Button" while running my GUI app
问题
我正在使用Python中的PySimpleGUI构建的项目,并提前创建了config.ini文件,用于设置窗口,以使用户能够更灵活地与程序交互。基本上,settings
是指这个文件。以下是一些错误,但第一次出现在下面提到的行。
第一个错误是
File "c:\Users\User\Desktop\Creating_YourOwn_Package\main.py", line 12, in settings_window [sg.Text("Separator"), sg.Input(settings["CSV"]["separator"], s=1, key="-SEPARATOR-"), TypeError: list indices must be integers or slices, not Button
英文:
I am working on a project built by PySimpleGUI in Python and I'd created config.ini file in advance for settings window so that user could interact with program more flexibly. Basically, settings
refers to this file. There were some following errors but the first occurence is in the below mentioned line.
def settings_window(settings):
layout = [[sg.Text("SETTINGS")],
[sg.Text("Separator"), sg.Input(settings["CSV"]["separator"], s=1, key="-SEPARATOR-"),
sg.Text("Decimal"), sg.Combo(settings["CSV"]["decimal"].split("|"),
default_value = settings["CSV"]["decimal_default"],
s=1, key="-DECIMAL-"),
sg.Text("Sheet Name:"), sg.Input(settings["Excel"]["sheet_name"], s=20, key="-SHEET_NAME-")]
[sg.Button("Save Current Settings", s=20)]]
window = sg.Window(title="Settings", layout = layout, modal=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == "Save Current Settings":
settings["CSV"]["separator"] == values["-SEPARATOR"]
settings["CSV"]["decimal"] == values["-DECIMAL-"]
settings["Excel"]["sheet_name"] == values["-SHEET_NAME-"]
sg.popup_no_titlebar("Settings saved!")
break
window.close()
if __name__ == "__main__":
SETTINGS_PATH = Path.cwd()
settings = sg.UserSettings(
path = SETTINGS_PATH, filename="config.ini",
use_config_file=True,
convert_bools_and_none=True
)
theme = settings["GUI"]["theme"]
font_family = settings["GUI"]["font_family"]
font_size = int(settings["GUI"]["font_size"])
sg.theme(theme)
sg.set_options(font=(font_family, font_size))
main_window()
The first error I came across is
File "c:\Users\User\Desktop\Creating_YourOwn_Package\main.py", line 12, in settings_window
[sg.Text("Separator"), sg.Input(settings["CSV"]["separator"], s=1, key="-SEPARATOR-"),
TypeError: list indices must be integers or slices, not Button
答案1
得分: 1
你忘记在第二个列表的末尾添加一个“,”。
[[sg.Text("SETTINGS")],
[sg.Text("Separator"), sg.Input(settings["CSV"]["separator"], s=1, key="-SEPARATOR-"),
sg.Text("Decimal"), sg.Combo(settings["CSV"]["decimal"].split("|"),
default_value=settings["CSV"]["decimal_default"],
s=1, key="-DECIMAL-"),
sg.Text("Sheet Name:"), sg.Input(settings["Excel"]["sheet_name"], s=20, key="-SHEET_NAME-")], # 在这里你忘记了逗号
[sg.Button("Save Current Settings", s=20)]]
所以,Python认为你试图访问第二个列表的特定索引,而不是添加一个列表。这就是为什么它会抛出TypeError错误。
现在应该已经修复了。
英文:
You forgot to add a "," add the end of your second list.
[[sg.Text("SETTINGS")],
[sg.Text("Separator"), sg.Input(settings["CSV"]["separator"], s=1, key="-SEPARATOR-"),
sg.Text("Decimal"), sg.Combo(settings["CSV"]["decimal"].split("|"),
default_value = settings["CSV"]["decimal_default"],
s=1, key="-DECIMAL-"),
sg.Text("Sheet Name:"), sg.Input(settings["Excel"]["sheet_name"], s=20, key="-SHEET_NAME-")], # YOU FORGOT COMA HERE
[sg.Button("Save Current Settings", s=20)]]
So instead of adding a list, python thinks you are trying to access a specific index of the second list. That is why it's throw a TypeError.
Now it should be fixed
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论