Tk Listbox Question

Kevin Cazabon kcazabon at home.com
Thu Dec 9 15:26:53 EST 1999


It's actually pretty simple... most of what you need can be found in Fred's
"Introduction to Tkinter" at:
http://www.pythonware.com/library/tkinter/introduction

Here's a quickie though.  It's in a class wrapper because I've build myself
a 'standard' set of compound widgits that I use.  You could call this by
simply calling:  "list = display_list(parent, items_in_list)"


#######################################################################
class display_list(Frame):
    def __init__(self, parent, items, height = 15, width = 20):
        self.frame = Frame(parent)
        self.frame.pack(side = TOP, expand = NO)

        self.listbox = Listbox(self.frame, height = height, width = width)
        self.listbox.pack(side = LEFT, expand = NO, fill = X)

        self.scrollbar = Scrollbar(self.frame)
        self.scrollbar.pack(side = RIGHT, expand = NO, fill = Y)

        self.listbox.config(yscrollcommand=self.scrollbar.set, relief =
SUNKEN)
        self.scrollbar.config(command = self.listbox.yview, relief = SUNKEN)

        for item in items:
            self.listbox.insert(items.index(item)+1,
str(items.index(item)+1) + '. ' + item)

        self.listbox.update_idletasks()

        #Here's the bindings

        self.listbox.bind('<Double-Button-1>', self.do_this)
        self.listbox.bind('<Control-Button-1>', self.do_that)
        self.listbox.bind('<Button-2>', self.do_something_else)

    def do_this(self):
        pass
    def do_that(self):
        pass
    def do_something_else(self):
        pass
############################################################################
###






"Nick Belshaw" <nickb at earth.ox.ac.uk> wrote in message
news:384FC9F6.1521A533 at earth.ox.ac.uk...
> Calling Tk wizards-
>
> Could anyone (/F?) tell me if it's possible to modify - without too much
> difficulty - the bindings on a Tk Listbox. What I would like to do is
> select an item with either Left or Right Mouse Buttons but with
> different actions on depending on which was used.
>
> doable or too tricky?
>
> or dead easy and I'm thick!
>
> thanks for any ideas
> Nick/Oxford
>
>
>





More information about the Python-list mailing list