Python get focused entry name

北战南征 提交于 2019-12-23 13:21:18

问题


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?

Here is my code:

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()

回答1:


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()



回答2:


Remember that when you call print, you are getting the representation of an object, not necessarily the object itself. To show you what's going on, add this to your get_focus1 function:

print("focus object class:", window2.focus_get().__class__)

You should see that it is indeed returning a reference to an Entry widget, meaning you can call all the normal methods on that object.



来源:https://stackoverflow.com/questions/27867754/python-get-focused-entry-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!