如何让 pygtk 条目只接受浮点数?
我希望用户只能输入浮点数(即从 0 到 9 的数字,并且小数点应该显示,其他字符不应该显示)。我怎样才能做到这一点?
编辑 我需要获取像“4”或“3.5”或“.9”这样的值,像“10e23”这样的
值也应该被拒绝,例如“10.12.45”......
I want the user to be able to type only a float (that is, numbers from 0 to 9 and the decimal point should show, other characters should not). How can I do that ?
edit
I need to get values like "4" or "3.5" or ".9", nothing like "10e23"
Inconsistent values should also be rejected, such as "10.12.45" ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
init 部分中的 SO 问题“How to extract a float number from a string in Python”,
我终于得到了一个可以接受的答案,并且:
连接到条目的“changed”事件。非常感谢所有提供帮助的人。
I've finally an acceptable answer thanks to SO question "How to extract a floating number from a string in Python"
in the init part,
and :
connected to the "changed" event of the entry. Many thanks to all those who helped.
我不确定您是否会从“已更改”事件中获取缓冲区。
但是,查看连接到信号可能是个好主意 "preedit-changed" 在条目本身中:
然后,当您获得输入时,您可以检查它是否有效,然后相应地操作字段值。
I'm not sure if you will get the buffer from the "changed" event.
However, it might be a good idea to have a look at connecting to the signal "preedit-changed" in the entry itself:
And then when you get the input you can check so that it is valid, and then manipulate the fields value accordingly.
捕获当用户修改条目中的值时触发的事件,并手动验证它是浮点数。这就像在
float()
周围使用try
一样简单:如果发生异常,则拒绝该值。
编辑:您也可以在输入字符时对其进行过滤。
Catch the event fired when the user modifies the value in the entry, and manually validate it's a float. This can be as easy as using a
try
aroundfloat()
:If an exception occurs, reject the value.
Edit: you can filter the characters as they are entered, too.