Python Forum
[Tkinter] moniter which widget triggerd event - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] moniter which widget triggerd event (/thread-24840.html)



moniter which widget triggerd event - catlessness - Mar-06-2020

I use tkinter and have several entries
now i'm using
frame.bind('<Key>',func)
to monitor event,
Q:can this function func tell me which entry had been changed and point a variable to it like:
window=tk.Window()
frame=tk.Frame(window)
def func():
    pointer={an entry that has been changed}
a=tk.Entry(...)
b=tk.Entry(...)
c=tk.Entry(...)
frame.bind('<Key>',func)
Thank you!!!


RE: moniter which widget triggerd event - Larz60+ - Mar-07-2020

Please post a snippet that we can be run.


RE: moniter which widget triggerd event - catlessness - Mar-07-2020

the problem is I dont really know if there is this function to focus on the entry changed, this is the closest i can give:
import tkinter as tk
window=tk.Tk()
window.geometry('450x500')
frame=tk.Frame(window,height=500,width=500)
frame.place(x=0,y=0)
def func(var):
    print('the following one is the problem, i dont know if it has this function')
    #pointer={an entry that has been changed}
a=tk.Entry(frame,show=None,textvariable=tk.StringVar(value=1))
b=tk.Entry(frame,show=None,textvariable=tk.StringVar(value=2))
c=tk.Entry(frame,show=None,textvariable=tk.StringVar(value=3))
a.place(x=20,y=20)
b.place(x=20,y=40)
c.place(x=20,y=60)
window.bind('<Key>',func)
window.mainloop()



RE: moniter which widget triggerd event - Larz60+ - Mar-07-2020

in the bind statement, <key> needs to be replaced with what you want to bind to,
and func needs to be replaces with the name of a function that you want to run when the event occurs.
Since you are not using a class, the 'func' must be defined with a 'def' prior to the bind statement which you have done.
you need to capture the event as an attribute of 'func' (I'd choose a better name here) and finally
the bind must be for a widget, not a window

assume you wanted to use left mouse button to bind to Entry a when enter button is pressed, the statement should look like:
a.bind('<Return>', func)



RE: moniter which widget triggerd event - catlessness - Mar-07-2020

I actually want to monitor any key event, and on any entry in the window, that why i used '<Key>' and window.bind()
the purpose is to run a parameter assignment function whenever there are values(in the entry) changed. But there are different functions when some special entries changed, thats why i need to know which entry changed.


RE: moniter which widget triggerd event - Larz60+ - Mar-07-2020

OK, misunderstood.
see example here: https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
section: Capturing keyboard events