鱼C论坛

 找回密码
 立即注册
查看: 2072|回复: 6

tkiner Entry focus问题

[复制链接]
发表于 2017-10-23 11:13:15 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Jiang_yt_ 于 2017-10-23 13:58 编辑

可能是我的问题没有描述清楚,就比如下面的程序,为什么第一次执行就没有问题(比如1+2 =3),然后再接着修改成11+22 = 程序就没有反应了??
#coding=utf-8
#!/usr/bin/env python

from tkinter import *

root = Tk()

def checkInt(content):

    # try:
    #     content = int(content)
    # except ValueError:
    #     print('请输入数字')
    # except:
    #     print('Unexpected error:', sys.exc_info()[0])
    # if isinstance(content, int):
    #     print('正确')
    #     return True
    # else:
    #     print('错误')
    #     print(content)
    #     return False
    return content.isdigit()

def sumTest():
    sumNum.delete(0, END)
    if not num1.get().isdigit():
        num1Value = '0'
    else:
        num1Value= num1.get()
    if not num2.get().isdigit():
        num2Value = '0'
    else:
        num2Value = num2.get()
    print(str(int(num1Value) + int(num2Value)))
    sumValue.set(str(int(num1Value) + int(num2Value)))
    return True

def sumCal():
    sumNum.delete(0, END)
    if not num1.get().isdigit():
        num1Value = '0'
    else:
        num1Value= num1.get()
    if not num2.get().isdigit():
        num2Value = '0'
    else:
        num2Value = num2.get()
    print(str(int(num1Value) + int(num2Value)))
    sumValue.set(str(int(num1Value) + int(num2Value)))
    return True


check = root.register(checkInt)

sumValue = StringVar()

num1 = Entry(root, width = 9, validate = 'key', validatecommand = (check, '%P'))
l1 = Label(root, text = '+')
num2 = Entry(root, width = 9, validate = 'key', validatecommand = (check, '%P'))
l2 = Label(root, text = '=')
# sumNum = Entry(root, width = 9, state = 'readonly', textvariable = sumValue)

sumNum = Entry(root, width = 9, state = 'readonly', textvariable = sumValue, validate = 'focusin', validatecommand = sumTest)
# button1 = Button(root, text = '计算', width = 8, command = sumCal)
# button1.grid(row = 1, column = 2, pady = 5)
num1.grid(row = 0, column = 0, padx = 10, pady = 10)
l1.grid(row = 0, column = 1, padx = 10, pady = 10)
num2.grid(row = 0, column = 2, padx = 10, pady = 10)
l2.grid(row = 0, column = 3, padx = 10, pady = 10)
sumNum.grid(row = 0, column = 4, padx = 10, pady = 10)



mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-10-23 12:53:01 | 显示全部楼层
请问你的返回值呢???
2017-10-23_125205.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-23 12:54:10 | 显示全部楼层
你设置的validate格式就是 'focusout'
其意思是当entry组件失去焦点的时候才进行验证,
你可以改为"focus" 或者'all'
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-23 13:39:35 | 显示全部楼层
ba21 发表于 2017-10-23 12:53
请问你的返回值呢???

可能是我的问题没有描述清楚,就比如下面的程序,为什么第一次执行就没有问题(比如1+2 =3),然后再接着修改成11+22 = 程序就没有反应了??
#coding=utf-8
#!/usr/bin/env python

from tkinter import *

root = Tk()

def checkInt(content):

    # try:
    #     content = int(content)
    # except ValueError:
    #     print('请输入数字')
    # except:
    #     print('Unexpected error:', sys.exc_info()[0])
    # if isinstance(content, int):
    #     print('正确')
    #     return True
    # else:
    #     print('错误')
    #     print(content)
    #     return False
    return content.isdigit()

def sumTest():
    sumNum.delete(0, END)
    if not num1.get().isdigit():
        num1Value = '0'
    else:
        num1Value= num1.get()
    if not num2.get().isdigit():
        num2Value = '0'
    else:
        num2Value = num2.get()
    print(str(int(num1Value) + int(num2Value)))
    sumValue.set(str(int(num1Value) + int(num2Value)))
    return True

def sumCal():
    sumNum.delete(0, END)
    if not num1.get().isdigit():
        num1Value = '0'
    else:
        num1Value= num1.get()
    if not num2.get().isdigit():
        num2Value = '0'
    else:
        num2Value = num2.get()
    print(str(int(num1Value) + int(num2Value)))
    sumValue.set(str(int(num1Value) + int(num2Value)))
    return True


check = root.register(checkInt)

sumValue = StringVar()

num1 = Entry(root, width = 9, validate = 'key', validatecommand = (check, '%P'))
l1 = Label(root, text = '+')
num2 = Entry(root, width = 9, validate = 'key', validatecommand = (check, '%P'))
l2 = Label(root, text = '=')
# sumNum = Entry(root, width = 9, state = 'readonly', textvariable = sumValue)

sumNum = Entry(root, width = 9, state = 'readonly', textvariable = sumValue, validate = 'focusin', validatecommand = sumTest)
# button1 = Button(root, text = '计算', width = 8, command = sumCal)
# button1.grid(row = 1, column = 2, pady = 5)
num1.grid(row = 0, column = 0, padx = 10, pady = 10)
l1.grid(row = 0, column = 1, padx = 10, pady = 10)
num2.grid(row = 0, column = 2, padx = 10, pady = 10)
l2.grid(row = 0, column = 3, padx = 10, pady = 10)
sumNum.grid(row = 0, column = 4, padx = 10, pady = 10)



mainloop()

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

使用道具 举报

 楼主| 发表于 2017-10-23 13:39:52 | 显示全部楼层
BngThea 发表于 2017-10-23 12:54
你设置的validate格式就是 'focusout'
其意思是当entry组件失去焦点的时候才进行验证,
你可以改为"focus ...

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

使用道具 举报

发表于 2017-10-23 14:53:22 | 显示全部楼层
Jiang_yt_ 发表于 2017-10-23 13:39
可能是我的问题没有描述清楚,就比如下面的程序,为什么第一次执行就没有问题(比如1+2 =3),然后再接着 ...

都放这里可否???check = root.register(checkInt)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-23 15:25:37 | 显示全部楼层
ba21 发表于 2017-10-23 14:53
都放这里可否???check = root.register(checkInt)

前面两个数值的check没有问题,检验OK,理论上是发生第3个focus后就会更新一下数据,但是问题是第一次第3个entry更新ok, 然后后续就没反应了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-25 12:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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