tkinter 文本输入验证 [英] tkinter text entry validation

查看:53
本文介绍了tkinter 文本输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python/tkInter 验证文本输入

I'm trying to validate the entry of text using Python/tkInter

def validate_text():
    return False

text = Entry(textframe, validate="focusout", validatecommand=validate_text)

validate_text 是函数的地方 - 我试过总是返回 False 并且总是返回 True 并且结果没有区别..?我需要在函数中包含一组参数吗?

where validate_text is the function - I've tried always returning False and always returning True and there's no difference in the outcome..? Is there a set of arguments in the function that I need to include?

编辑 - 从 NONE 更改为 focusout...仍然无法正常工作

Edit - changed from NONE to focusout...still not working

推荐答案

我认为您唯一缺少的是 invalidcommand(或 invcmd).如果validatecommand(或vcmd)返回false,您希望它做什么?根据 Tk 手册(见下文),如果 vcmd 返回 False 并且 validate not 设置为 none,则将调用 invcmd.invcmd 的典型命令是 Tkinter.bell,它会发出叮当的声音.另请注意,vcmd 和 invcmd 非常敏感,如果它们遇到异常,如果在 vcmd 或 invcmd 函数内以任何方式更改了小部件,或者如果 vcmd 没有返回,则将验证变为无"一个有效的 Tcl 布尔值.特别是 textvariable 因引起问题而臭名昭著,条目中名为 valdation 的部分 专门处理这个问题.

I think the only thing your missing is an invalidcommand (or invcmd). What are you expecting validatecommand (or vcmd) to do if it returns false? According to the Tk Manual (see below), if vcmd returns False and validate is not set to none then invcmd will be called. The typical command for invcmd is Tkinter.bell, which makes a ding sound. Also note that vcmd and invcmd are very touchy, and will turn validate to 'none' if they encounter an exception, if the widget is changed in anyway inside the vcmd or invcmd functions or if vcmd does not return a valid Tcl boolean. In particular textvariable is notorious for causing issues, and a section in Entry called valdation specifically deals with that.

以下是来自 Tk 命令条目的相关部分(与 Spinbox 相同).请参阅下文以获取更多参考.

Here are the relevant portions from Tk Command Entry (same for Spinbox). See below for more references.

命令行名称:-validatecommand 或 -vcmd
数据库名称:validateCommand
数据库类:ValidateCommand
指定要验证输入小部件的输入时要评估的脚本.将其设置为 {} 会禁用此功能(默认).该命令必须返回一个有效的 Tcl 布尔值.如果它返回 0(或等效的有效 Tcl 布尔值),则意味着您拒绝新版本并且它不会发生并且如果设置了 invalidCommand 将被评估.如果它返回 1,则出现新版本.有关详细信息,请参阅下面的验证.

Command-Line Name: -validatecommand or -vcmd
Database Name: validateCommand
Database Class: ValidateCommand
Specifies a script to eval when you want to validate the input into the entry widget. Setting it to {} disables this feature (the default). This command must return a valid Tcl boolean value. If it returns 0 (or the valid Tcl boolean equivalent) then it means you reject the new edition and it will not occur and the invalidCommand will be evaluated if it is set. If it returns 1, then the new edition occurs. See Validation below for more information.

命令行名称:-invalidcommand 或 -invcmd
数据库名称:invalidCommand
数据库类:InvalidCommand
指定当 validateCommand 返回 0 时要评估的脚本.将其设置为 {} 会禁用此功能(默认).此选项的最佳用途是将其设置为 bell.有关详细信息,请参阅下面的验证.

Command-Line Name: -invalidcommand or -invcmd
Database Name: invalidCommand
Database Class: InvalidCommand
Specifies a script to eval when validateCommand returns 0. Setting it to {} disables this feature (the default). The best use of this option is to set it to bell. See Validation below for more information.

看看这个SO答案Tk 命令epydoc-Tkinter 以获取更多参考.

Have a look at this SO answer, the Tk commands and epydoc-Tkinter for more references.

这个问题有很多重复.Python tkInter 入门乐趣
限制 Tkinter Entry 小部件中的值

这篇关于tkinter 文本输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆