Yesterday, I searched all over trying to figure out how to properly use the "listvariable" argument with tk's Listbox class. Unfortunately, no amount of searching (online) could come up with anything more useful than telling me the variable needed to be a list, and nothing built-in exists.
I finally came across a way of using it that I consider fairly simple, and wanted to toss it out there for everyone else. One post I saw mentioned using StringVar. That just didn't work well in my head, so instead I used its parent class: Variable. Apparently, the trick is to use tuples. If I use a list, string, etc - the Listbox seems to split the content based on spaces. Not the behavior I was looking for as I have spaces in the strings I wish to display. If anyone has better suggestions, I'd love to hear them. (I thought about a ListVar class, but haven't made it much beyond the thought). Regardless, I hope the following code helps others avoid the confusion I went through. Sample code: import Tkinter, tkSimpleDialog from Tkconstants import * class ListboxTest( tkSimpleDialog.Dialog ): ############################################################################# def __init__( self, master, tupleItems = () ): self.myVar = Tkinter.Variable() self.myVar.set( tupleItems ) tkSimpleDialog.Dialog.__init__( self, master, "Listbox testing" ) ############################################################################# def body( self, master ): lbox = Tkinter.Listbox( master, listvariable = self.myVar ) lbox.grid( row = 0, column = 0, sticky = N + W ) self.myVar.set( self.myVar.get() + tuple( [ "***** Final string being added *****" ] ) ) print type( self.myVar.get() ), self.myVar.get() if( "__main__" == __name__ ): tk = Tkinter.Tk() lt = ListboxTest( tk, tuple( [ "String 1 - with some spaces", "String 2 - with more spaces" ] ) )
-- http://mail.python.org/mailman/listinfo/python-list