我试图让 tkinter 返回在列表框中单击的项目的索引。这是我的代码。
def fileSelection(self):
selection = listbox.curselection
print(selection)
listbox.bind("<Button-1>", fileSelection)
现在它打印
bound method Listbox.curselection of tkinter.Listbox object at 0x00320E30
无论点击什么项目。如果我更改代码以包含这样的按钮:
button = Button(text=u"test", command=OnButtonClick)
def OnButtonClick():
selection = listbox.curselection()
print(selection)
并选择列表框项目,然后单击按钮,它将按预期打印所选项目的索引,但这是我不想要的额外步骤。
最佳答案
def fileSelection(self):
selection = listbox.curselection
print(selection)
看起来你忘记了括号。
def fileSelection(self):
selection = listbox.curselection()
print(selection)
关于python - 返回在 tkinter 列表框中单击的项目的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31012566/