Skip to content Skip to sidebar Skip to footer

How To Have A Tkinter Entry Box Repeat A Function Each Time A Character Is Inputted?

I am trying to create an basic email client for fun. I thought that it would be interesting if the password box would show random characters. I already have a function for creating

Solution 1:

Unfortunately, the show attribute of the Entry widget doesn't work that way: as you've noticed, it simply specifies a single character to show instead of what characters were typed.

To get the effect you want, you'll need to intercept key presses on the Entry widget, and translate them, then. You have to be careful, though, to only mutate keys you really want, and leave others (notably, Return, Delete, arrow keys, etc). We can do this by binding a callback to all key press events on the Entry box:

self.Password.bind("<Key>", callback)

where callback() is defined to call your random function if it's an ascii letter (which means numbers pass through unmodified), insert the random character, and then return the special break string constant to indicate that no more processing of this event is to happen):

def callback(event):
    if event.char in string.ascii_letters:
        event.widget.insert(END, random_char())
        return "break"

Post a Comment for "How To Have A Tkinter Entry Box Repeat A Function Each Time A Character Is Inputted?"