鱼C论坛

 找回密码
 立即注册
查看: 1870|回复: 3

[已解决]为什么我的validatecommand 在输入不是数字的情况下没有触发

[复制链接]
发表于 2020-12-29 20:57:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# 产生一个除法计算器
from tkinter import *
import math

root = Tk()
root.geometry( '600x200+100+100' )
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
v1.set( 0 )
v2.set( 0 )
v3.set( 0 )

def Test(num):
    if num.isdigit():
        return True
    else:
        return False
Test=root.register(Test)
def f1():
    Frame( root, text='莫为难小弟了' )
def f2():
    pass

e1 = Entry( root, textvariable=v1, validate='focusout',\
            validatecommand=(Test,'% P'),invalidcommand=f1 )  # 为什么我的validatecommand 在输入不是数字的情况下没有触发
e2 = Entry( root, textvariable=v2, validate='focusout', validatecommand=(Test,'% P'), invalidcommand=f1 )
e3 = Entry( root, textvariable=v3 )
e1.grid( row=0, column=0, padx=10, pady=10)
e2.grid( row=0, column=2 )
e3.grid( row=0, column=4)
e3['state'] = 'readonly'
Label( root, text='/' ).grid( row=0, column=1 )
Label( root, text='=' ).grid( row=0, column=3 )
Label( root, text='请输入要进行计算的数值' ).grid( row=1, sticky='n' )

Button( root, text='确认计算' ,command=f2 ).grid( row=2, column=0, padx=10, pady=10, sticky='w' )
Button( root, text='取消' , command=root.quit ).grid( row=2, column=0, padx=10, pady=10, sticky='e')
mainloop()


最佳答案
2020-12-30 09:57:53
本帖最后由 小伤口 于 2020-12-30 10:18 编辑
  1. # 产生一个除法计算器
  2. from tkinter import *
  3. import math

  4. root = Tk()
  5. root.geometry( '600x200+100+100' )
  6. v1 = StringVar()
  7. v2 = StringVar()
  8. v3 = StringVar()
  9. v1.set( 0 )
  10. v2.set( 0 )
  11. v3.set( 0 )

  12. def Test(num):
  13.     if num.isdigit():
  14.         return True
  15.     else:
  16.         return False
  17. Test=root.register(Test)
  18. def f1():
  19.     Frame( root, text='莫为难小弟了' )
  20. def f2():
  21.     pass

  22. e1 = Entry( root, textvariable=v1, validate='key',\
  23.             validatecommand=(Test,'%P') )  # 为什么我的validatecommand 在输入不是数字的情况下没有触发
  24. e2 = Entry( root, textvariable=v2, validate='key', validatecommand=(Test,'%P') )
  25. e3 = Entry( root, textvariable=v3 )
  26. e1.grid( row=0, column=0, padx=10, pady=10)
  27. e2.grid( row=0, column=2 )
  28. e3.grid( row=0, column=4)
  29. e3['state'] = 'readonly'
  30. Label( root, text='/' ).grid( row=0, column=1 )
  31. Label( root, text='=' ).grid( row=0, column=3 )
  32. Label( root, text='请输入要进行计算的数值' ).grid( row=1, sticky='n' )

  33. Button( root, text='确认计算' ,command=f2 ).grid( row=2, column=0, padx=10, pady=10, sticky='w' )
  34. Button( root, text='取消' , command=root.quit ).grid( row=2, column=0, padx=10, pady=10, sticky='e')
  35. mainloop()
复制代码

我帮你改好了(以上代码仅供参考)

1 %P就是%P中间不能留空格
2 这是键盘事件要用key
3  invalidcommand=f1中的Frame还没有安排位置不然会报错,所以我把invalidcommand删了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-30 09:57:53 | 显示全部楼层    本楼为最佳答案   
本帖最后由 小伤口 于 2020-12-30 10:18 编辑
  1. # 产生一个除法计算器
  2. from tkinter import *
  3. import math

  4. root = Tk()
  5. root.geometry( '600x200+100+100' )
  6. v1 = StringVar()
  7. v2 = StringVar()
  8. v3 = StringVar()
  9. v1.set( 0 )
  10. v2.set( 0 )
  11. v3.set( 0 )

  12. def Test(num):
  13.     if num.isdigit():
  14.         return True
  15.     else:
  16.         return False
  17. Test=root.register(Test)
  18. def f1():
  19.     Frame( root, text='莫为难小弟了' )
  20. def f2():
  21.     pass

  22. e1 = Entry( root, textvariable=v1, validate='key',\
  23.             validatecommand=(Test,'%P') )  # 为什么我的validatecommand 在输入不是数字的情况下没有触发
  24. e2 = Entry( root, textvariable=v2, validate='key', validatecommand=(Test,'%P') )
  25. e3 = Entry( root, textvariable=v3 )
  26. e1.grid( row=0, column=0, padx=10, pady=10)
  27. e2.grid( row=0, column=2 )
  28. e3.grid( row=0, column=4)
  29. e3['state'] = 'readonly'
  30. Label( root, text='/' ).grid( row=0, column=1 )
  31. Label( root, text='=' ).grid( row=0, column=3 )
  32. Label( root, text='请输入要进行计算的数值' ).grid( row=1, sticky='n' )

  33. Button( root, text='确认计算' ,command=f2 ).grid( row=2, column=0, padx=10, pady=10, sticky='w' )
  34. Button( root, text='取消' , command=root.quit ).grid( row=2, column=0, padx=10, pady=10, sticky='e')
  35. mainloop()
复制代码

我帮你改好了(以上代码仅供参考)

1 %P就是%P中间不能留空格
2 这是键盘事件要用key
3  invalidcommand=f1中的Frame还没有安排位置不然会报错,所以我把invalidcommand删了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-12-30 10:19:25 | 显示全部楼层
小伤口 发表于 2020-12-30 09:57
我帮你改好了(以上代码仅供参考)

1 %P就是%P中间不能留空格

# 产生一个除法计算器
from tkinter import *
import math

root = Tk()
root.geometry( '600x200+100+100' )
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
v1.set( 0 )
v2.set( 0 )
v3.set( 0 )

def Test1(num):
    print('检测开始')
    return num.isdigit()

Test=root.register(Test1)
def f1():
    root=Tk()
    Label(root,text='莫为难小弟了').pack()
    mainloop()
def f2():
    v3.set(str(float(v1.get())/float(v2.get())))#f2是如何得到v1、V2、v3的值的啊?

e1 = Entry( root, textvariable=v1, validate='focusout',\
            validatecommand=(Test,'%P'),invalidcommand=f1 )  # 切记分别为 validate validatecommend invalidcommend
e2 = Entry( root, textvariable=v2, validate='focusout', validatecommand=(Test,'% P'), invalidcommand=f1 )
e3 = Entry( root, textvariable=v3 )
e1.grid( row=0, column=0, padx=10, pady=10)
e2.grid( row=0, column=2 )
e3.grid( row=0, column=4)
e3['state'] = 'readonly'
Label( root, text='/' ).grid( row=0, column=1 )
Label( root, text='=' ).grid( row=0, column=3 )
Label( root, text='请输入要进行计算的数值' ).grid( row=1, sticky='n' )

Button( root, text='确认计算' ,command=f2 ).grid( row=2, column=0, padx=10, pady=10, sticky='w' )
Button( root, text='取消' , command=root.quit ).grid( row=2, column=0, padx=10, pady=10, sticky='e')
mainloop()

这是我修改完的代码,已经可以实现除法,但我还想请问一下f2是如何得到v1、V2、v3的值的啊?

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-30 10:25:51 | 显示全部楼层
v1.set( 0 )
v2.set( 0 )
v3.set( 0 )
这儿你定义了呀
就属于外部变量,函数可以调用
  1. >>> a=1
  2. >>> def haha():
  3.         c=a+5
  4.         return c

  5. >>> haha()
  6. 6
  7. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 14:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表