0

I am working on a program where I create some widgets in a for loop. So I need to get the name of them dynamically. I have set up is when the mouse enters the frame. Which holds the two text label widgets. I causes a function to run. And I want to change the background color of a widget with the name of noteName. But I seem to have run into a stopping point and I can no figure it out. I have searched online but could not find much. SO does anyone here know how to get the name of a widget?

Code:

def get_children_hover(event):
        for widgets in event.widget.winfo_children():
            #This is here where I can not seem to figure out how to get the widgets name.

Can someone push me into the right direction.

1
  • Do you mean you want to get the text of the label or do you want to see if widgets is a label? Commented Nov 26, 2014 at 20:15

1 Answer 1

2

winfo_children() is the right thing to use but you are using it wrong. It is a method for parent widgets. (i.e. root, frame, canvas etc..)

Also:

If the order doesn’t matter, you can get the same information from the children widget attribute (it’s a dictionary mapping Tk widget names to widget instances, so widget.children.values() gives you a list of instances).

simple example:

import tkinter as tk

def foo():
    print ("Frame:", frm.winfo_children())
    print ("Root:", root.winfo_children())
    print ("children_values:", root.children.values())

root = tk.Tk()
frm = tk.Frame(root)
tk.Label(root,text="foo").pack()
btn = tk.Button(frm,text="FOOO",command=foo)
frm.pack()
btn.pack()

root.mainloop()

about your code:

def get_children_hover(event):
    for widgets in root.winfo_children(): #assuming your Tk() instance named root
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.