Tkinter Entry only allows digit number

本文介绍如何使用Python的Tkinter库实现输入验证,并详细解释了如何在Tkinter组件中应用验证命令来确保用户输入的数据符合特定条件,如只能输入数字。通过实例演示了如何配置验证命令和Tkinter Entry组件的验证过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from Tkinter import *
def validatecontent(S):
    print "S='%s'" % S
    return (S.isdigit()==bool(S))   #can only input digit number,and must use this format  **S**==**S**
vcmd=(top.register(validatecontent),'%S')   #use S,the text string being inserted
# valid percent substitutions (from the Tk entry man page)
# %d = Type of action (1=insert, 0=delete, -1 for others)
# %i = index of char string to be inserted/deleted, or -1
# %P = value of the entry if the edit is
# %s = value of entry prior to editing
# %S = the text string being inserted or deleted, if any
# %v = the type of validation that is currently set
# %V = the type of validation that triggered the callback
#      (key, focusin, focusout, forced)
# %W = the tk name of the widget
entry2=Entry(top,textvariable=e2,validate='all',validatecommand=vcmd)
entry2.pack()
top.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值