For one, don't use lambda
. It's useful for a narrow range of problems and this isn't one of them. Create a proper function, they are much easier to write and maintain.
Once you do that, you can call curselection
to get the current selection. You say you tried that but your example code doesn't show what you tried, so I can only assume you did it wrong.
As for the rather unusual advice to use nearest
... all it's saying is that bindings you put on a widget happen before default bindings for that same event. It is the default bindings that set the selection, so when you bind to a single button click, your binding fires before the selection is updated by the default bindings. There are many ways around that, the best of which is to not bind on a single click, but instead bind on <<ListboxSelect>>
which will fire after the selection has changed.
You don't have that problem, however. Since you are binding on a double-click, the selection will have been set by the default single-click binding and curselection
will return the proper value. That is, unless you have your own single-click bindings that prevent the default binding from firing.
Here's a simple example that prints out the selection so you can see it is correct. Run it from the command line so you see stdout:
import Tkinter as tk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
lb = tk.Listbox(self)
lb.insert("end", "one")
lb.insert("end", "two")
lb.insert("end", "three")
lb.bind("<Double-Button-1>", self.OnDouble)
lb.pack(side="top", fill="both", expand=True)
def OnDouble(self, event):
widget = event.widget
selection=widget.curselection()
value = widget.get(selection[0])
print "selection:", selection, ": '%s'" % value
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…