<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt">Hi,<br><br>I'm trying to make a small improvement on a data entry program and it is literally giving me a headache. I would appreciate your help or suggestions. <br><br>The actual program uses Autocomplete entry widgets [1], which is a subclass of the Tkinter Entry widget. The sample code below uses a simple Entry widget, for the sake of simplicity. The autocompletions are recorded in a dictionary of the form {entry name: set(&lt;entries&gt;)}. The problem is that entries with typos cannot be deleted, so wrong autocomplete suggestions ("Bbbilly Gates") are given until the end of time. My solution: I want to bind each entry widget to the Delete key, which makes it possible to remove the typo-entry from the set of entries. I am using an ' expanded event handler' [2] to do the event
 binding.<br><br>The sample code below creates two entry widgets. The problem is that the entry contents is not retrieved correctly. If I fill the 2nd entry with some text, then hit 'Del' , it shows the content of the *first* entry. Also, I would like to isolate the event handler into its own function, not as a function within a function, but I'm not quite sure how.<br><br><span>[1] <a target="_blank" href="http://tkinter.unpythonic.net/wiki/AutocompleteEntry">http://tkinter.unpythonic.net/wiki/AutocompleteEntry</a></span><br><span>[2] <a target="_blank" href="http://www.daniweb.com/code/snippet306072.html">http://www.daniweb.com/code/snippet306072.html</a></span><br><br>from Tkinter import *<br><br>def createWidgets(veldnamen):<br>&nbsp;&nbsp;&nbsp; root=Tk()<br>&nbsp;&nbsp;&nbsp; termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), "*Postcode": set(["2600AA", "8000NN"])}<br>&nbsp;&nbsp;&nbsp; handleDels = {}<br>&nbsp;&nbsp;&nbsp; for veldnaam
 in veldnamen:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # tcl names must start with lowercase letter<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entryWidget=Entry(root, name=veldnaam[0].lower() + veldnaam[1:])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entryWidget.grid()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def handleDel(event, widget=entryWidget, root=root, termenlijst=termenlijst):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vensternaam = str(root.focus_get())[1:].capitalize() # ... and back to uppercase<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if vensternaam.startswith("*"):&nbsp;&nbsp;&nbsp; # mandatory fields start with '*' in my program.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vensternaam = "*" + vensternaam[1:].capitalize()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vensterinhoud =
 entryWidget.get()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "Naam", vensternaam&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # entry name<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "Inhoud", vensterinhoud&nbsp;&nbsp; # entry contents<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; termenlijst[vensternaam].remove(vensterinhoud)&nbsp; # here's where the typo is removed<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except KeyError:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass # user tries to delete a term that doesn't exist in the termenlijst.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; handleDels[entryWidget] = handleDel<br>&nbsp;&nbsp;&nbsp; # do all the bindings (is this where it goes
 wrong??)<br>&nbsp;&nbsp;&nbsp; for entryWidget, handleDel in handleDels.iteritems():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entryWidget.bind("&lt;Delete&gt;", handleDel)<br>&nbsp;&nbsp;&nbsp; print handleDels<br>&nbsp;&nbsp;&nbsp; print termenlijst<br><br>createWidgets(["Naam", "*Postcode"])<br><div>&nbsp;<br>Thanks again for having a look at this.<br><br></div>Cheers!!<br>Albert-Jan<br><br><div>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<div><br></div></div>
</div><br>

      </body></html>