Home > OS >  I want to add functionality User is not allowed to enter 0 as the first number in the Entry field
I want to add functionality User is not allowed to enter 0 as the first number in the Entry field

Time:06-30

I want to add functionality User is not allowed to enter 0 as the first number in the Entry field, please help me to solve the problem.

vcmd = (root.register(self.enter_only_digits),  '%P', '%d')
        self.text_num = tk.Entry(validate='key', validatecommand=vcmd)
        self.text_num.place(x=100, y=210, width=100, height=20)

def enter_only_digits(self, entry, action_type) -> bool:
        if action_type == '1' and not entry.isdigit():
            return False

        return True

CodePudding user response:

def enter_only_digits(self, entry, action_type) -> bool:
        if action_type == '1' and not entry.isdigit():
            return False
        if entry[0] == "0":
           print("not allowed to enter 0 as first number")

        return True

CodePudding user response:

since the entry is a string it can be checked if starting index contains 0 as:

def enter_only_digits(self, entry, action_type) -> bool:
        if action_type == '1' and not entry.isdigit() or entry[0] == "0":
            return False

        return True
  • Related