Searching for a ComboBox for Tkinter?

H

Harlin Seritt

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)
 
M

Martin Franklin

Harlin said:
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:

Why not extend an existing Tkinter widget? This way you get at
least the geometry (pack, grid etc) methods thown in for free!

class ComboBox(Frame):
def __init__(self, parent, *args, **kw):
Frame.__init__(self, parent, *args, **kw)

You could extend Entry above is just an example...

Also worth pointing out are a couple of ComboBox's out there already
Pmw includes one as does Tix (IIRC)

Cheers,
Martin.
 
M

Martin Franklin

Martin said:
Harlin said:
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:


Why not extend an existing Tkinter widget? This way you get at
least the geometry (pack, grid etc) methods thown in for free!

class ComboBox(Frame):
def __init__(self, parent, *args, **kw):
Frame.__init__(self, parent, *args, **kw)

You could extend Entry above is just an example...

Also worth pointing out are a couple of ComboBox's out there already
Pmw includes one as does Tix (IIRC)

Cheers,
Martin.
I forgot to say good work! and you may be interested in the Tkinter
mailing list and Wiki (where you could post your 'mega' widget as an example

http://mail.python.org/mailman/listinfo/tkinter-discuss
gmane.comp.python.tkinter
http://tkinter.unpythonic.net/wiki/

(There seems to be a lot of Tkinter related posts recently)


Martin
 
R

Raseliarison nirinA

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

how about:
ComboBox - an Entry field with a dropdown menu. The user can select a
choice by either typing in the entry subwdget or selecting from
the listbox subwidget.

Subwidget Class
--------- -----
entry Entry
arrow Button
slistbox ScrolledListBox
tick Button
cross Button : present if created with the fancy option
 
H

Harlin Seritt

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
 
H

Harlin Seritt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top