Python Forum
[Tkinter] moniter which widget triggerd event
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] moniter which widget triggerd event
#1
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!!!
Reply
#2
Please post a snippet that we can be run.
Reply
#3
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()
Reply
#4
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)
Reply
#5
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.
Reply
#6
OK, misunderstood.
see example here: https://effbot.org/tkinterbook/tkinter-e...ndings.htm
section: Capturing keyboard events
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] What can the event.widget reference be used for? Knickers 4 2,397 Aug-26-2021, 02:17 PM
Last Post: Knickers
  pygtk2, how to disconnect all callback of widget or window event ? harun2525 1 3,262 Feb-19-2017, 11:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020