[Tutor] Binding the mouse to a list using Tk.
BELSEY, Dylan
dylan.belsey@baesystems.com
Tue, 15 Oct 2002 08:32:37 +0930
Only had a brief look at your problem, but I believe that the Tkinter
widgets Listbox and ScrolledListBox may be what you need (and save you quite
a bit of time). You can use the Listbox methods from within the
ScrolledListbox object as well. You could then possibly associate a
function with your button which gets the currently selected item.
getcurselection() returns the text while the curselection() will return the
index.
A lot of this is quite similar to what your code has proposed but using
the built-in widgets and their functions may shed some light on an easier
solution.
HTH,
Dylan
-----Original Message-----
From: andy surany [mailto:mongo57a@comcast.net]
Sent: Tuesday, 15 October 2002 04:20
To: tutor@python.org
Subject: [Tutor] Binding the mouse to a list using Tk.
Hi all!
What I want to do is use a button to capture the position/value of the
selected item in a scrolled list. So the user selects the item in the list
with a single click of the left mouse button and then clicks a button.
Should be easy - I'm just not getting it......
I'm using Tkinter. I Created a class (ScrolledList) for a scrolled list
which works fine. Created
another class (aaaaa) which populates the list - and it works fine. Created
a handler under ScrolledList (handleList) which should trap the results of
the bind (makeWidgets). Right now, all I'm trying to do is just print out
the value (just testing..) - but my logic is incorrect (actually, I think
that the logic may be correct - it's just in the wrong place...).
Here is a synopsis of the code. The "...." is non-relevant code which I have
removed for simplification.
class ScrolledList(Frame):
def __init__(self, options, parent=None):
........
self.makeWidgets(options)
def handleList(self, event):
index = self.listbox.curselection()
label = self.listbox.get(index)
print "label is ", label
def makeWidgets(self, options):
........
list.bind('<Button-1>', self.handleList)
Class aaaaa
def update_strategy_code(self):
..........
ScrolledList(options)
Button(self, text='Update',
command=self.edit_strategy_code).pack(side=LEFT)
def edit_strategy_code(self):
label=self.listbox.get(ACTIVE)
print 'info2=',label
TIA!
Andy (mongo57a@comcast.net)