[Tutor] Binding the mouse to a list using Tk.

alan.gauld@bt.com alan.gauld@bt.com
Tue, 15 Oct 2002 12:18:04 +0100


> I'm using Tkinter. I Created a class (ScrolledList) for 
> a scrolled list which works fine. 

So you get the message printed when you click on an item?
If so then it might be a good idea to have a state variable 
to store the current selection?

another class (aaaaa) which populates the list - and it works fine. 

the bind (makeWidgets). Right now, all I'm trying to do is just print out
the value (just testing..) - but my logic is incorrect 

So the print isn't working? I'm not sure what exactly is happening 
here.

class ScrolledList(Frame):
    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) 

You dont show the listbox being created,but I assume its 
a standard Tk Listbox control? And that you pass the same 
parent as the Scrolled Listbox? (BTW Its conventional 
to have the parent parameter as the first one after self....)

Class aaaaa
    def update_strategy_code(self):
        ..........
        ScrolledList(options)
        Button(self, text='Update',
        command=self.edit_strategy_code).pack(side=LEFT)

This confuses me too. You are creating a Button with parent 
of self yet the aaaaa class has no parent parameter(thats shown...)
Also you are creating the ScrolledList class with a parent of 
None - this is likely to lead to some very strange behaviour!


    def edit_strategy_code(self):
        label=self.listbox.get(ACTIVE)

Which listbox is this? Its not the one in your ScrolledList, 
thats for sure. It was just a local object in update_strategy...
Or maybe its now an orphaned control parented on None?

I really don't see how this is supposed to work?
Which is the parent form - I assume aaaaa? If so create the 
sub controls(ScrolledList) as children of self and assign 
them to instance variables for easy access. Its a much 
more conbventional and maintainable way of working. This 
seems to rely on a tad too much magic for my liking!

OTOH I'm maybe just confused and missing something obvious.

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld