Newbie scoping problem

David Miller dmillerON at MAPSmote.rsn.com
Wed May 12 15:42:39 EDT 1999


Fredrik Lundh <fredrik at pythonware.com> wrote:
> David Miller wrote:
>> And the handleList method is pretty simple:
>> 
>>     def handleList(self, event):
>>         index = self.listbox.curselection()           # on listbox double-click
>>         label = self.listbox.get(index)               # fetch selection text
>>         print index, label                            # and call action here

> without actually running your code, I suspect
> this is the culprit.  curselection doesn't do what
> you expect:

I copied it from some examples in "Programming Python", and it
works pretty much OK, even if it needs to be revisited for the
reasons you stated.

Thanks to Michael P. Reilly for pointing out the real problem...

After I determined the real problem was that the last browser
window I opend was handling all the dbl-click events, Michael
suggested the following code:

    def handleList(self, event):
        listbox = event.widget                         
        index = listbox.curselection()
        print 'index =', index


... which worked like a charm.  I just had to point listbox
at the widget with the event and it worked like I meant it to:)




> http://www.pythonware.com/library/tkinter/introduction/listbox.htm


I love this site!  This is a terrific resource!

> in other words, you need to treat the return value as a
> list, and the list members as strings (don't think this is
> changed in 1.5.2).  see the patterns section on the
> above page for some more sample code.

True, but the real problem was that when I clicked on window
A, window B was handling the event and correctly reporting that
none of its entries were selected:)

> non-portable.  try something like:

> for file in os.listdir("/etc"):
>     listbox.insert(END, file)

> or if you feel fancy:

> apply(listbox.insert, (END,) + tuple(os.listdir("/etc")))

These are great suggestions.  I'll definately run with them:)

Thanks to Michael and Fredrik!!!!

--- David






More information about the Python-list mailing list