我想添加功能用户不允许输入0作为条目字段中的第一个数字

发布于 2025-02-11 13:34:29 字数 426 浏览 1 评论 0原文

我想添加功能用户不允许输入0作为条目字段中的第一个数字,请帮助我解决问题。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2025-02-18 13:34:29
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
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
雾里花 2025-02-18 13:34:29

由于该条目是字符串,如果启动索引包含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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文