I've created a ghetto-ized ComboBox that should work nicely for Tkinter
(unfortunately no dropdown capabilities yet).

I've found why it's such a pain in the @ss to create one. You have to
re-create common methods and make sure they're mapped to the right
component (inside this widget are frame, entry, listbox, and scrollbar
widgets). I tried to look for ways I could inherit some methods from
other widgets but couldn't think of how to do it in an efficient way.

If anyone has suggestions to improve it, please respond... Remember...
I'm a traditional c/c++ turned python programmer who forgets that there
usually is a better, faster more graceful way to do things with Python
than with C/C++/Java.

Cheers,

Harlin

######################################################################
# Seritt Extensions:
# Date: 02262005
# Class ComboBox
# Add this section to your Tkinter.py file in 'PYTHONPATH/Lib/lib-tk/'
# Options: width, border, background, foreground, fg, bg, font
#               , relief, cursor, exportselection, selectbackgroun,
selectforeground, height
#
# Methods: activate(int index) => int, curselection() => int,
delete(item=["ALL" or int], start=int, end=["END" or
# int],
#       focus_set()/focus(), get()=>selected string in box, pack(padx=int,
pady=int,  fill(X, Y, BOTH), expand=bool, # side=LEFT,
#       RIGHT, TOP, BOTTOM, CENTER
#

class ComboBox:
        ITEMS = range(0)

        def activate(self, index):
                self.listbox.activate(index)

        def curselection(self):
                return map(int, self.listbox.curselection())[0]

        def delete(self, item=None, start=None, end=None):
                if item=='ALL':
                        self.listbox.delete(0, END)
                elif start == None and end == None:
                        self.listbox.delete(item)
                else:
                        self.listbox.delete(start, end)

        def get_focus(self):
                self.entry.get_focus()

        def focus(self):
                self.entry.get_focus()

        def get(self):
                return self.entry.get()

        def pack(self, padx=None, pady=None, fill=None, expand=None,
side=None):
                self.frame.pack(padx=padx
                   , pady=pady
                   , fill=fill
                   , expand=expand
                   , side=side)

        def size(self):
                return self.listbox.size()

        def insert(self, START, ITEM):
                self.ITEMS.append(ITEM)
                self.listbox.insert(START, ITEM)
                self.listbox.select_set(0)
                self.entry.delete(0, END)
                self.entry.insert(0, 
self.listbox.get(self.listbox.curselection()))

        def change_entry(self, event):
                def i(event):
                        try:
                                self.entry.delete(0, END)
                                self.entry.insert(0, 
self.listbox.get(self.listbox.curselection()))
                        except:
                                pass
                self.listbox.bind("<ButtonRelease-1>", i)

        def __init__(self, parent, width=None, border=1, background=None,
foreground=None, fg=None, bg=None, font=None
                , relief=None, cursor=None, exportselection=None,
selectbackgroun=None, selectforeground=None, height=None):
                self.frame = Frame(parent)
                self.entry = Entry(self.frame
                        , width=None
                        , border=border
                        , background=background
                        , foreground=foreground
                        , fg=fg
                        , bg=bg
                        , font=font
                        , relief=relief
                        , cursor=cursor
                        , exportselection=exportselection
                        , selectbackgroun=selectbackgroun
                        , selectforeground=selectforeground
                        , height=height)
                self.entry.pack(fill=X)
                self.scroll = Scrollbar(self.frame)
                self.scroll.pack(side=RIGHT, fill=Y)
                self.listbox = Listbox(self.frame
                        , yscrollcommand=self.scroll.set
                        , width=None
                        , border=border
                        , background=background
                        , foreground=foreground
                        , fg=fg
                        , bg=bg
                        , font=font
                        , relief=relief
                        , cursor=cursor
                        , exportselection=exportselection
                        , selectbackgroun=selectbackgroun
                        , selectforeground=selectforeground
                        , height=height)
                self.listbox.pack(fill=X)
                self.scroll.config(command=self.listbox.yview)
                self.listbox.bind("<ButtonPress-1>", self.change_entry)

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to