Tkinter Entry does not respond to regex conditions

I have a tkinter menu which works fine except for one textbox where I’m limiting the input to a string containing one letter and one digit. Here’s part of the code:

root = Tk()
root.title('word search')

# only one char one digit eg. t3
def midLett(v):
  return re.match('^[a-zA-Z]\d$', v) is not None

# register function on root
fwrapper = (root.register(midLett), '%P')

# mid-letter
Label(root, text = "enter a letter and its position in word eg. '3t' means word must contain letter 't' in position 3:", wraplength=170).place(x=10, y=10)

midletterTxt = StringVar()
Entry(root, textvariable=midletterTxt, validate='key', validatecommand=fwrapper).place(x=30, y=100)


The problem is the textbox does not accept anything. I’m expecting to type something like ‘t5’. Though the required format looks simple I can’t understand why it does not accept anything. Please help!

How do you type in “t5”?

First you type “t”, but the entry isn’t allowed to contain just “t” because it doesn’t match the regex, so that’s rejected.

Do you see the problem?

1 Like

Yes indeed, so obvious. It would only work when entering the required string at once with pasting but that has no benefit.