返回在tkinter列表框中单击的项目的索引

Vol*_*sch 2 python listbox tkinter selection

我试图让tkinter返回列表框中单击的项目的索引.这是我的代码.

def fileSelection(self):
    selection = listbox.curselection
    print(selection)

listbox.bind("<Button-1>", fileSelection)
Run Code Online (Sandbox Code Playgroud)

现在它打印

绑定方法在0x00320E30处的tkinter.Listbox对象的Listbox.curselection

无论点击什么项目.如果我更改代码以包含这样的按钮:

button = Button(text=u"test", command=OnButtonClick)

def OnButtonClick():
    selection = listbox.curselection()
    print(selection)
Run Code Online (Sandbox Code Playgroud)

并选择列表框项目,然后单击按钮,它将按预期打印所选项目的索引,但这是我不想要的额外步骤.

Kev*_*vin 7

def fileSelection(self):
    selection = listbox.curselection
    print(selection)
Run Code Online (Sandbox Code Playgroud)

看起来你忘记了括号.

def fileSelection(self):
    selection = listbox.curselection()
    print(selection)
Run Code Online (Sandbox Code Playgroud)