Python tk Listbox: -listvariable class (on win XP)

Looney, James B james.b.looney at ulalaunch.com
Mon Jan 11 20:34:28 EST 2010


I mentioned that I figured out how to use a variable with a Python TK Listbox in my post (http://www.mail-archive.com/python-list@python.org/msg271288.html).

Now, I'm trying to make a class, ListVar, that allows me to operate on a Listbox's listvariable as if it were a list.  The problem is, it doesn't work as expected some of the time.  If I try to add a sequence of simple strings, it doesn't display anything.  If I convert them to a tuple or instantiate a class (with __str__ defined), then it displays everything - the simple string, instances, tuples, everything.

I don't understand why it won't show the simple strings, by themselves, in the Listbox.  Notice - the spaces are important.  I can make this work without as much of a headache my strings don't have spaces (which is what the 2 or 3 examples I've been able to find did), but spaces apparently makes this more complicated.

Can anyone explain what's happening?  I'm completely frustrated with trying to figure this out.
Thanks,
-JB

Here is the simplest working code snippet I felt I could make:

[code]
import Tkinter, tkSimpleDialog

class ListVar( Tkinter.Variable, list ): # Dual inheritance
   def __init__( self, master = None, *args, **kw ):
      Tkinter.Variable.__init__( self, master )
      list.__init__( self, *args, **kw )
      self.set( self ) # populate the Tk variable

   def get( self ):
      value = Tkinter.Variable.get( self )
      if( isinstance( value, list ) ):
         return value
      return list( value )

   def set( self, value ):
      if( isinstance( value, list ) ):
         value = tuple( value )
      Tkinter.Variable.set( self, value )

   def append( self, item ): list.append( self, item ), self.set( self )

# Class to wrap around a string and make a simple instance.
class sillyString( object ):
   def __init__( self, s ): self.myString = s
   def __str__( self ): return self.myString

# Dialog class to display a Listbox and test the ListVar class.
class ListboxDialog( tkSimpleDialog.Dialog ):
   def __init__( self, master, listItems = [] ):
      self.myVar = ListVar( master, listItems ) # Initial set of the list
      tkSimpleDialog.Dialog.__init__( self, master, "Listbox testing" )

   def body( self, master ):
      Tkinter.Listbox( master, listvariable = self.myVar, width = 50 ).grid()
      self.myVar.append( "appended string" ) # test append

   def __str__( self ): return "%s" % self.myVar.get()

if( "__main__" == __name__ ):
   tk = Tkinter.Tk()
   tk.withdraw()
   # The spaces are important as my listbox will contain strings with spaces.
   # The print displays what's in the ListVar after the dialog exits
   print ListboxDialog( tk, '' ) # Simple String
   print ListboxDialog( tk, [ '' ] ) # list with empty string
   print ListboxDialog( tk, [ 'abc string' ] ) # list with short string
   print ListboxDialog( tk, [ sillyString( "instance 1" ) ] ) # class instance
   print ListboxDialog( tk, [ sillyString( "instance 1" ),
                              sillyString( "instance 2" ) ] ) # 2 instances
   print ListboxDialog( tk, [ ( "A tuple", ),
                              "A string" ] )            # tuple and string
[/code]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100111/f68653c7/attachment.html>


More information about the Python-list mailing list