Retrieve Tkinter listbox item by string, not by index

Kevin Walzer kw at codebykevin.com
Fri Dec 22 15:31:42 EST 2006


I'm trying to set the active item in a Tkinter listbox to my 
application's currently-defined default font.

Here's how I get the fonts loaded into the listbox:

  self.fonts=list(tkFont.families())
  self.fonts.sort()

   for item in self.fonts:
             self.fontlist.insert(END, item)   #self.fontlist is the 
ListBox instance


So far, so good. But I don't know how to set the active selection in the 
listbox to the default font. All the methods for getting or setting a 
selection in the listbox are based on index, not a string. And using 
standard list search methods like this:

        if "Courier" in self.fontlist:
             print "list contains", value
         else:
             print value, "not found"

returns an error:

TypeError: cannot concatenate 'str' and 'int' objects

So I'm stuck. Can someone point me in the right direction?
-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com



More information about the Python-list mailing list