Sample code usable Tkinter listbox

Alf P. Steinbach alfps at start.no
Sun Feb 28 19:30:12 EST 2010


In case Someone Else(TM) may need this.

This code is just how it currently looks, what I needed for my code, so it's not 
a full-fledged or even tested class.

But it works.


<code language="Py3">
import tkinter as t
import tkinter.simpledialog
import tkinter.messagebox
t.askstring     = tkinter.simpledialog.askstring
t.warningbox    = tkinter.messagebox.showwarning

class UsableListbox( t.Frame ):
     def __init__( self, parent_widget ):
         t.Frame.__init__( self, parent_widget )
         scrollbar = t.Scrollbar( self, orient = "vertical" )
         self.lb = t.Listbox( self, yscrollcommand = scrollbar.set )
         scrollbar.config( command = self.lb.yview )
         scrollbar.pack( side = "right", fill = "y" )
         self.lb.pack( side = "left", fill = "both", expand = 1 )

     def current_index( self ):
         indices = self.lb.curselection()
         assert( len( indices ) <= 1 )       # TODO: about multi-selection.
         return None if len( indices ) == 0 else int( indices[0] )

     def current( self ):
         #return self.lb.get( "active" )     # Incorrect with mousing
         i = self.current_index()
         return "" if i is None else self.lb.get( i )

     def append( self, item ):
         return self.lb.insert( "end", item )

     def add_selection_event_handler( self, handler ):
         "An event handler takes one argument, a Tkinter Event"
         return self.lb.bind( "<<ListboxSelect>>", handler )
</code>


Cheers,

- Alf



More information about the Python-list mailing list