Tkinter bug in Entry widgets on OS X

Peter Otten __peter__ at web.de
Sat Sep 1 06:30:43 EDT 2012


Arnaud Delobelle wrote:

> On Friday, 31 August 2012, Dennis Lee Bieber wrote:
> 
>> On Fri, 31 Aug 2012 15:41:01 GMT, Alister
>> <alister.ware at ntlworld.com<javascript:;>
>> >
>> declaimed the following in gmane.comp.python.general:
>>
>> > I agree that it is unexpected in a single line entry box but isn't the
>> 1st
>> > rule of user interface design to assume the user is a moron & will do
>> > things they are not supposed to do?
>> >
>> > Therefore invalid inputs should be handled gracefully (not just insert
>> > random characters) which is what I think the original poster is
>> > suggesting.
>>
>>         To which I'd suggest the programmer (vs the user), probably needs
>> to
>> code some sort of per-character validation check... After all, there may
>> be situations where accepting pretty much any key-code is desired, and
>> if the widget filters characters before they get to the programmer level
>> that becomes impossible.
>>
>>
> It would be good if I could intercept the key press event and cancel its
> action on the Entry widget.  It's easy to intercept the key event, but I
> haven't found out how to prevent the funny characters from being inserted
> into the Entry widget input area.  

Untested as I have no Mac:

import Tkinter as tk

def suppress(event):
    if event.keycode in {111, 116}:
        print "ignoring", event.keycode
        return "break"
    print event.keycode, "accepted"

root = tk.Tk()
entry = tk.Entry(root)
entry.bind("<Key>", suppress)
entry.pack()

root.mainloop()

> I've struggled to find good tkinter
> docs on the web.

For my (basic) needs I keep coming back to

http://infohost.nmt.edu/tcc/help/pubs/tkinter/




More information about the Python-list mailing list