是否可以限制 TextCtrl 仅在 wxPython 中接受数字?
我想要一个只接受数字的文本控件。 (只是整数值,如 45 或 366)
执行此操作的最佳方法是什么?
I want to have a text control that only accepts numbers. (Just integer values like 45 or 366)
What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我必须做类似的事情,检查字母数字代码。 EVT_CHAR 上的提示是正确的:
I had to do something similar, checking for alphanumeric codes. The tip on EVT_CHAR was the right thing:
IntCtrl
、Masked Edit Control
和NumCtrl
都旨在实现此目的,并具有不同级别的控制。查看“更多 Windows/控件”下的 wx 演示,了解它们是如何工作的。(或者,如果您确实希望直接使用原始 TextCtrl 执行此操作,我认为您需要捕获 EVT_CHAR 事件,测试字符,并调用 evt.Skip() (如果它是允许的字符)。)
IntCtrl
,Masked Edit Control
, andNumCtrl
are all designed to do just this, with different levels of control. Checkout the wx demo under "More Windows/Controls" to see how they work.(Or, if you're instead really looking forward to doing this directly with a raw TextCtrl, I think you'd want to catch EVT_CHAR events, test the characters, and call evt.Skip() if it was an allowed character.)
您可以尝试
IntCtrl
、EVT_CHAR
或实现新的/现有的验证器(例如 IntValidator)。验证器可用于验证字段(在尝试验证对话框/面板上的多个内容时很有用),它们还可以与 EVT_CHAR 一起使用来限制字段中的输入。You can try
IntCtrl
,EVT_CHAR
, or implement a new/existing validator (like IntValidator). Validators can be used to validate a field (useful when trying to validate multiple things on a dialog/panel) and they can also be used with EVT_CHAR to restrict input in a field.正如其他答案所指出的,可以使用
EVT_CHAR
处理程序来完成此操作。您需要为要允许通过的字符调用event.Skip()
,而不是为要阻止的字符调用它。一个细微差别是您可能还想为制表符调用 event.Skip() ;按 Tab 键会触发EVT_CHAR
事件,如果您不调用event.Skip()
,您将有效禁用TextCtrl
之间的 Tab 键遍历s。这是一个最小的应用程序,显示两个接受整数或小数的
TextCtrl
,并具有工作选项卡遍历功能:As other answers note, it's possible to do this with an
EVT_CHAR
handler. You'll want to callevent.Skip()
for the characters you want to let through, and not call it for the ones you want to block. One nuance is that you probably also want to callevent.Skip()
for tab characters; pressing tab triggers theEVT_CHAR
event, and if you don't callevent.Skip()
, you'll effectively disable tab traversal betweenTextCtrl
s.Here's a minimal application showing two
TextCtrl
s that accept integer or decimal numbers, with working tab traversal:我想要相同的但对于浮点数,所以我在类中使用了以下方法:
在构造函数中注册事件:
修改上面的答案
I wanted the same but for floats, so I used the following method in the class:
to register the event in the constructor:
Modifying the answer above
NumCtrl 对我来说有一些奇怪的怪癖。这是我尝试创建基于 EVT_CHAR 和键码的数字控件。
该控件允许数字以及所有特殊键码(ctrl组合、箭头键、退格键等...),因此复制粘贴、撤消重做、全选等仍然有效。它只会阻止其他可打印字符(使用 string.printable )和 unicode 字符(使用 WXK_NONE< /a>)
另一种检查并允许所有特殊键码的方法可以通过此答案找到。这是一种更好的方法,但需要更多代码。
NumCtrl has some weird quirks to me. Here is my attempt to create a Number control based on EVT_CHAR and keycode.
This control allow numbers as well as all special keycode (ctrl combo, arrow key, backspace etc...), so that copy-paste, undo-redo, select-all etc still works. It will only block other printable characters (using string.printable) and unicode characters (using WXK_NONE)
Another way to check and allow all special keycode can be found by this answer. This is a better approach but requires more code.
请检查 wxpython 演示中的“Validator.py”脚本。这正是你所需要的
Please check "Validator.py" script in wxpython demo. it is exactly what you need