Tkinter focus_get()
Matthew Dixon Cowles
matt at mondoinfo.com
Sat Mar 10 17:18:42 EST 2001
On Sat, 10 Mar 2001 14:39:57 -0600, dsavitsk <dsavitsk at e-coli.net>
wrote:
>I have a grid (actually a dictionary of dictionaries) of entry
>widgets that are dynamically created. Each has <KeyPress> bound to
>it. I am trying to determine which one has focus, so i used
>self.root.focus_get()
>which returns something that looks like
>.8854796.8898804.18282492.18282532.18283228.18248316.17887444.17854708
>is there a way to interpret this? or a better way to get focus?
That's the actual Tk widget, it just prints as a string. You can
compare it with the widgets you've created:
def reportUp(self,event):
w=self.tkRoot.focus_get()
print w
print type(w)
if w is self.entry1:
print "It's e1"
elif w is self.entry2:
print "It's e2"
But you probably don't need the focus_get() call because the widget
attribute of the event object ought to be the same:
def reportUp(self,event):
if event.widget is self.e1:
print "It's e1"
elif event.widget is self.e2:
print "It's e2"
An exhaustive search like this simple and very practical if the number
of widgets is small. If it grows large, one practical thing to do is
to save a reference to the widget in a callable object at the time the
widget is created:
class reportUp:
def __init__(self,widget):
self.w=widget
return None
def __call__(self,eventIgnored):
self.w.insert(0,"Me") # Or something more useful
return None
And when widgets are being created:
self.e1=Entry(self.tkRoot,text="one")
self.e1.pack(side=TOP)
self.e1.bind('<KeyPress-Up>',reportUp(self.e1))
Regards,
Matt
More information about the Python-list
mailing list