tags:

views:

18

answers:

1

I have a list of n Entry widgets. The user should be able to type only the following charaters: "V", "F", " ". If the user types one of those characters, the focus should pass from Entry #x to Entry #x+1, otherwise the focus should stay where it is (on Entry #x) and the input should be discarded.

I am not able to discard the wrong input: if the user presses a key different from those allowed, the Entry field get that key, but the command .delete(0,END) doesn't work, as the widget itself hasn't yet memorized the key pressed.

How could I do?

+1  A: 
import Tkinter as tk

def keyPress(event):
    if event.char in ('V', 'F', ' '):
        print event.char
    elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
        print event.keysym
        return 'break'


root = tk.Tk()
entry = tk.Entry()
entry.bind('<KeyPress>', keyPress)
entry.pack()
entry.focus()

root.mainloop()

You can easily break up the statement so it will go to a different form based on the key.

The event.keysym part is in there so you can ALT-F4 to close the app when you're in that widget. If you just else: return 'break' then it will capture all other keystrokes as well.

This also is a case sensitive capture. If you want case insensitive, just change it to event.char.upper()

Wayne Werner
Thank you, I was missing the "return 'break'" part. If I insert another linea before it (even a simple print statement), it doesn't work.
zar
@zar, You're welcome - `return 'break'` is how you stop Tkinter events from propagating (i.e. button presses, etc.).
Wayne Werner
Actually, what I said isn't true. If before the "break" I inserted another line, which calls the .get() statement over the Entry widget. That call was messing up everything. Thanks again.
zar
I have another question: how can I get the value of the Entry field? A line like x=entry.get() doesn't give the correct value, if the cursor is still on the entry field.
zar
(I will post it as a separate question)
zar