检查Entry Widget Tkinter中的数字输入

时间:2017-07-10 06:37:19

标签: python tkinter widget tkinter-entry

我有几个问题 1)我试图将Entry小部件仅限于数字输入。我已经看到堆栈溢出的一些示例,但他们倾向于使用基于类的Tkinter编码,我正在做它通常的风格。

E=Entry(t3, bg='gray', textvariable=weekly_savings[num], validate='focus', validatecommand=MoneyValidation))
I am not sure how to implement this money validation. The window code is as follows
t3=Toplevel(root)
bg='gold'
t3.title(u"\u092c\u0939\u0940 \u0916\u0924\u093e")
t3.geometry('800x450+100+50')
t3.transient(root)
t3.configure(background=bg)
t3.overrideredirect(True)

其次,我正在与半文盲人群合作完成最终用例,我们只想使用会计师键盘。 我想将焦点转移绑定到num lock键。我该怎么做?

2 个答案:

答案 0 :(得分:0)

如果你的目标是只允许数字出现在Entry小部件中,你可以使用Bryan Oakley详述的方法this answer

def MoneyValidation(S):
    if S in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
        return True
    t3.bell() # .bell() plays that ding sound telling you there was invalid input
    return False

vcmd = (t3.register(MoneyValidation), '%S')
E = Entry(t3, bg='gray', validate='key', vcmd=vcmd)
E.pack()

答案 1 :(得分:0)

def MoneyValidation(S):
    if S.isdigit():
        return True
    else:
        return False

reg = t3.register(MoneyValidation

E = Entry(t3, bg='gray', validate='key',validatecommand=(reg, %P))
E.pack()

参考:https://www.youtube.com/watch?v=oRYshQCOHOs