Python Forum
[Tkinter] How Does Entry Verification Work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How Does Entry Verification Work?
#2
The problem with only calling once appears to be related to returning None. I changed you callback to this and it is called for each keystroke.
def validatePhone(num):
    print('called')
    pattern=re.compile('\d{3}-\d{3}-\d{4}')
    ok =  pattern.search(num)
    print('got here', num, ok)
    return True if ok else False
To verify I tried both of these for validate. Returning None behaves as you saw. Called only once then validation no longer used. Returning False runs each time the key is pressed.
def validateFalse(num):
    print(num)
    return False

def validateNone(num):
    print(num)
    return None
So your validation function needs to return True or False. But you still have a problem. The only way you can enter a phone number is using copy/paste. Typing will never match your pattern because 1 digit does not match your pattern and typing in "555-867-5309" requires you first type "5".

I don't know how to write a regex that would match '5', '55', '555' and '555-' but not '55-', '5-' or '-'. This works.
def validatePhone(num):
    pattern = 'ddd-ddd-dddd'
    if len(num) > len(pattern):
        return False
    for digit, chr in zip(num, pattern):
        if chr == 'd':
            if not digit.isdigit():
               return False
        elif digit != chr:
            return False
    return True
Reply


Messages In This Thread
How Does Entry Verification Work? - by gw1500se - Nov-13-2021, 08:43 PM
RE: How Does Entry Verification Work? - by deanhystad - Nov-13-2021, 10:49 PM
RE: How Does Entry Verification Work? - by gw1500se - Nov-17-2021, 02:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,612 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,628 Mar-18-2019, 05:36 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020