By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,629 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,629 IT Pros & Developers. It's quick & easy.

Searching for a ComboBox for Tkinter?

P: n/a
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)

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Or if you prefer you can download it here:
http://www.seritt.org/pub/tkinter/Tkinter-03132005.py

Replace your Tkinter.py with this one. Make sure you back up your old
one in case you decide mine sucks.

Thanks,

Harlin

Jul 18 '05 #2

P: n/a
Thanks nirinA... I've played with that one before. I'm not a big fan of
Pmw or Tix so much I guess although when it gets down to it, those
'extra' toolkits are probably more functional.

Cheers,

Harlin

Jul 18 '05 #3

P: n/a
ahh Frame! I didn't even think of extending Frame. I kept wondering how
I could instead extend entry and listbox... thanks for the pointer.
Yeah I know there are others out there, I just wanted to create one
from tkinter widgets and keep the constructor as close to other tkinter
widgets as possible. I notice that when you use Pmw you have different
option names and methods that are a bit inconsistent (not too hard to
remember though--it's not a criticism of Pmw or Tix). I am very
narrow-minded. :-)

As far as the other lists/wiki, I will be looking at them.

Thanks,

Harlin

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.