Edgar Runnman
Edgar Runnman

Reputation: 183

python tkinter event not working for entry within a frame

I have an entry that's within a frame. I want to pass an event from that entry.

Here is code that works, but does not include an entry in a frame:

import tkinter as tk
myUi= tk.Tk()
myFrame = tk.Frame(myUi)
myFrame.pack()
def printMe(event):
    value = event.widget.get()
    print(value)
myEntry = tk.Entry(myUi,name='entry')
myEntry.bindtags(('.entry','Entry'))
myEntry.bind("<KeyRelease>", printMe)
myEntry.pack()
myUi.mainloop()

Here is code that doesn't work:

import tkinter as tk
myUi= tk.Tk()
myFrame = tk.Frame(myUi)
myFrame.pack()
def printMe(event):
    value = event.widget.get()
    print(value)
myEntry = tk.Entry(myFrame,name='entry')
myEntry.bindtags(('.entry','Entry'))
myEntry.bind("<KeyRelease>", printMe)
myEntry.pack()
myUi.mainloop()

Upvotes: 0

Views: 265

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386285

When you put the entry in a frame, it's binding tag contains it's own name plus the name of its ancestors separated by periods. In this specific case, the binding tag for the entry is .!frame.myentry. You can see this by printing out the default bindtags before you change them (eg: print(str(myEntry)))

Since you are changing the binding tags for the entry to be ('.entry', 'Entry'), any bindings on the widget itself (ie: on the binding tag .!frame.entry) will not be associated with the widget.

Upvotes: 1

Related Questions