Python 获取焦点条目名称 [英] Python get focused entry name

查看:21
本文介绍了Python 获取焦点条目名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在按下向上或向下箭头键时增加或减少条目值.为此,我需要首先找到焦点所在的条目,我正在尝试执行.focus_get()".问题是我不知道它是如何工作的或者它返回了什么.它为每个条目返回 1 个唯一编号,例如:.45191744",但每次运行该程序时该编号都会更改.以下数字是运行代码时的最后 5 次尝试..50518728".53009096".55889592".51891896"

I'm trying to make a entry value increase or decrease whenever the up or down arrow key is pressed. To do this i need to first find which entry that's in focus, and i'm trying to do that ".focus_get()". The problem is that i can't figure out how it works or what its returning. It is returning 1 unique number for each entry, something like: ".45191744" but this number changes each time i run the program. The following numbers is for the last 5 attempts, when running the code. ".50518728" ".53009096" ".55889592" ".51891896"

如何获取焦点条目的变量名称?

How can i get the variable name of the focused entry?

这是我的代码:

def get_focus1(event):
    print("return: event.widget is", event.widget)
    print("focus is:", window2.focus_get())
    print(window2.focus_get())
    print(help(window2.Entry))

window2 = Tk()

eyear1 = Entry(window2, width=4, font=("Helvetica", 16)) #  Entry for year
eyear1.insert(10, defaultYear)
eyear1.grid(row=1, column=1)

emonth1 = Entry(window2, width=4, font=("Helvetica", 16)) #  Entry for Month
emonth1.insert(10, defaultMonth)
emonth1.grid(row=1, column=2)

eday1 = Entry(window2, width=4, font=("Helvetica", 16)) #  Entry for day
eday1.insert(10, defaultDay)
eday1.grid(row=1, column=3)

window2.bind('<Left>', get_focus1)

mainloop()

推荐答案

focus_get 返回实际对象.你想要做的是,假设你没有使用 textvariable 有充分的理由(见 Bryan 的评论),是清除文本并重新编写新值(做一些验证显然).你最终的结果是这样的:

focus_get returns the actual object. What you want to do, assuming your not using textvariable for a good reason (see Bryan's comment), is to clear the text and re-write the new value (do some validation obviously). What you end up is something like this:

from tkinter import *

def up(event):
    # warning, exceptions can happen
    old = int(event.widget.get()) # this gives back the actual object!
    event.widget.delete(0, END) # delete existing text
    event.widget.insert(10, old + 1) # put new text in

def down(event):
    # warning, exceptions can happen
    old = int(event.widget.get()) # this gives back the actual object!
    event.widget.delete(0, END) # delete existing text
    event.widget.insert(10, old - 1) # put new text in

window2 = Tk()

eyear1 = Entry(window2, width=4, font=("Helvetica", 16)) #  Entry for year
eyear1.insert(10, 2015)
eyear1.grid(row=1, column=1)

emonth1 = Entry(window2, width=4, font=("Helvetica", 16)) #  Entry for Month
emonth1.insert(10, 1)
emonth1.grid(row=1, column=2)

eday1 = Entry(window2, width=4, font=("Helvetica", 16)) #  Entry for day
eday1.insert(10, 10)
eday1.grid(row=1, column=3)

# bind both keys to corresponding event handlers
window2.bind('<Up>', up)
window2.bind('<Down>', down)
mainloop()

这篇关于Python 获取焦点条目名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆