WxPython 将选择转换为数值 TypeError

WxPython converting choice to number value TypeError

所以我想这是一个相当简单的问题,我只是不明白其中的错误。这是我当前的代码:

        # SINGLE CHOICE INPUT
        choices = ["Auto", "Manual", "Manual (code only)"]
        chooseOneBox = wx.SingleChoiceDialog(None, "Setup / Opsætning", "Setup / Opsætning", choices)

        if chooseOneBox.ShowModal() == wx.ID_OK:
            setupChoice = choices.index[chooseOneBox.GetStringSelection()] + 1
            if setupChoice == 1:
                print(choices[setupChoice]-1)
            elif setupChoice == 2:
                print(choices[setupChoice]-1)
            print(choices[setupChoice])

所以我有列表 choices,其中包含一堆正确显示在 chooseOneBox 中的选项。尝试做时:
setupChoice = choices.index[chooseOneBox.GetStringSelection()] + 1 我收到以下错误:'builtin_function_or_method' object is not subscriptable

出于简单的原因,我想将字符串从 chooseOneBox 转换为整数。我如何避免出现该错误?

indexlist 的函数,即 returns 项目出现的第一个索引。

替换

choices.index[chooseOneBox.GetStringSelection()] + 1

有了这个

choices.index(chooseOneBox.GetStringSelection()) + 1

我看不出您执行该代码的方式有任何优势。
为什么不是这个:

choices = ["Auto", "Manual", "Manual (code only)"]
chooseOneBox = wx.SingleChoiceDialog(None, "Setup / Opsætning", "Setup / Opsætning", choices)

if chooseOneBox.ShowModal() == wx.ID_OK:
    setupChoice = choices.index(chooseOneBox.GetStringSelection())
    print(choices[setupChoice])

或者更直接地,使用 GetSelection() 其中 returns 所选项目的索引:

choices = ["Auto", "Manual", "Manual (code only)"]
chooseOneBox = wx.SingleChoiceDialog(None, "Setup / Opsætning", "Setup / Opsætning", choices)

if chooseOneBox.ShowModal() == wx.ID_OK:
    print(choices[chooseOneBox.GetSelection()])