0

Interactively validating Entry widget content in tkinter

the link above explains how to do validation.

I am trying to do the same thing. But somehow i am not able to do it.

It is a 10 digit string. The first two being alphabets, the next two numbers then again the next two will be alphabets and then the rest all numbers. for example MH02UH2012.

Other question which i have is when i run this the print of S and i is coming for the first three inputs only after that there is no print. and sometimes it prints only the first entered variable. I am not able to understand what is the issue

import tkinter
tk=tkinter.Tk()

def only_numeric_input(P,S,i):
    i = int(i)
    print (i, S)

    if S == " ":

        return False

    elif i < 2:
        if not S.isdigit():

            return True


    elif i > 5:

        if S.isdigit():

            return True
        else:

            return False
    elif i > 9:
        return False

e1=tkinter.Entry(tk)
e1.grid(row=0,column=0)
c=tk.register(only_numeric_input)
e1.configure(validate="key",validatecommand=(c,'%P', "%S", "%i"))
tk.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

1

There are issues in the condition statements of the only_numeric_input function. Try the below only_numeric_input function instead.

def only_numeric_input(P,S,i):
    i = int(i)
    print (i, S)
    if S == " ":
        return False

    if i < 2 or (i>3 and i<6):
        if S.isalpha() and S.isupper():
            return True
        else:
            return False
    elif i<10:
        if S.isdigit():
            return True
        else:
            return False
    elif i > 9:
        return False
Sun Bear
  • 7,594
  • 11
  • 56
  • 102