Newbie scoping problem

Fredrik Lundh fredrik at pythonware.com
Wed May 12 11:14:26 EDT 1999


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:

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

    curselection

    curselection(). Get a list of the currently selected
    alternatives. The list contains the indexes of the
    selected alternatives (beginning with 0 for the first
    alternative in the list). In Tkinter 1.101 (Python 1.5.1),
    the list contains strings instead of integers. Since
    this may change in future versions, you should make
    sure your code can handle either case. See the
    patterns section for a suggested solution.

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.

and yes, that os.system call is both inefficient and
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")))

</F>





More information about the Python-list mailing list