Reputation: 5
I am trying to create a combobox and an entry widget, which is dependent on the combobox. To be more precise: the user should select a value in the combobox and the entry widget serves data dependent on the combobox with an autocomplete function. I have found the autocomplete source code here:
http://code.activestate.com/recipes/578253-an-entry-with-autocompletion-for-the-tkinter-gui/
Here is what I have done so far:
from Tkinter import *
import ttk, os, re
root = Tk()
root.minsize(500,300)
root.maxsize(550,310)
class AutocompleteEntry(Entry):
def __init__(self, lista, *args, **kwargs):
Entry.__init__(self, *args, **kwargs)
self.lista = lista
print "lista in autocom " + str(lista)
self.var = self["textvariable"]
if self.var == '':
self.var = self["textvariable"] = StringVar()
self.var.trace('w', self.changed)
self.bind("<Right>", self.selection)
self.bind("<Up>", self.up)
self.bind("<Down>", self.down)
self.lb_up = False
def changed(self, name, index, mode):
if self.var.get() == '':
self.lb.destroy()
self.lb_up = False
else:
words = self.comparison()
print words
if words:
if not self.lb_up:
self.lb = Listbox()
self.lb.bind("<Double-Button-1>", self.selection)
self.lb.bind("<Right>", self.selection)
self.lb.place(x=self.winfo_x(), y=self.winfo_y()+self.winfo_height())
self.lb_up = True
self.lb.delete(0, END)
for w in words:
self.lb.insert(END,w)
else:
if self.lb_up:
self.lb.destroy()
self.lb_up = False
def selection(self, event):
if self.lb_up:
self.var.set(self.lb.get(ACTIVE))
self.lb.destroy()
self.lb_up = False
self.icursor(END)
def up(self, event):
if self.lb_up:
if self.lb.curselection() == ():
index = '0'
else:
index = self.lb.curselection()[0]
if index != '0':
self.lb.selection_clear(first=index)
index = str(int(index)-1)
self.lb.selection_set(first=index)
self.lb.activate(index)
def down(self, event):
if self.lb_up:
if self.lb.curselection() == ():
index = '0'
else:
index = self.lb.curselection()[0]
if index != END:
self.lb.selection_clear(first=index)
index = str(int(index)+1)
self.lb.selection_set(first=index)
self.lb.activate(index)
def comparison(self):
pattern = re.compile(self.var.get() + '.*')
return [w for w in self.lista if re.match(pattern, w)]
class MyListbox:
lista = ['a', 'actions', 'additional', 'also', 'an', 'and', 'angle', 'are', 'as', 'be', 'bind', 'bracket', 'brackets', 'button', 'can', 'cases', 'configure', 'course', 'detail', 'enter', 'event', 'events', 'example', 'field', 'fields', 'for', 'give', 'important', 'in', 'information', 'is', 'it', 'just', 'key', 'keyboard', 'kind', 'leave', 'left', 'like', 'manager', 'many', 'match', 'modifier', 'most', 'of', 'or', 'others', 'out', 'part', 'simplify', 'space', 'specifier', 'specifies', 'string;', 'that', 'the', 'there', 'to', 'type', 'unless', 'use', 'used', 'user', 'various', 'ways', 'we', 'window', 'wish', 'you']
def __init__(self, parent, title):
self.parent = parent
self.parent.title(title)
self.parent.protocol("WM_DELETE_WINDOW", self.closes)
self.cities = ['New York', 'Vienna', 'Miami', 'Oslo']
self.establishment()
def combobox_handler(self, event):
current = self.combobox.current()
print self.combobox.get()
self.entNumber.delete(0, END)
print self.lista
self.entNumber = AutocompleteEntry(self.lista, self.parent)
def establishment(self):
mainFrame = Frame(self.parent)
mainFrame.pack(fill=BOTH, expand=YES)
fr_left = Frame(mainFrame, bd=10)
fr_left.pack(fill=BOTH, expand=YES, side=LEFT)
self.combobox = ttk.Combobox(fr_left, values=self.cities)
self.combobox.bind('<<ComboboxSelected>>', self.combobox_handler)
self.combobox.pack()
fr_right = Frame(mainFrame, bd=10)
fr_right.pack(fill=BOTH, expand=YES, side=RIGHT)
fr_up = Frame(fr_right)
fr_up.pack(side=TOP, expand=YES)
self.entNumber = Entry()
self.entNumber.place(x=50, y=100)
def closes(self, event=None):
self.parent.destroy()
if __name__ == '__main__':
app = MyListbox(root, "Main Window")
root.mainloop()
The code is just for testing.
The problem is, that the program stops in the init of autocompleteEntry() without any error. Can someone give me a hint, what I am doing wrong?
Thank you in anticipation! Stefan
Upvotes: 0
Views: 5414
Reputation: 91007
Your issue is that after selecting an entry in the combobox, even though you are creating the AutocompleteEntry
, but you are not positioning it anywhere , so it is not showing up . You would need to place it somewhere for it to show up.
Example -
self.entNumber1 = AutocompleteEntry(self.lista, self.parent)
self.entNumber1.place(x=50, y=120)
To place it right below the entry called entNumber
.
Or you can simple create AutoCompleteEntry
from the start in self.entNumber
, but that all depends on what you are trying to achieve.
Upvotes: 1