Is there any way to find a widget's parent (specifically button) without saving the parent in an object attribute?
but = Button(main_window, text = "close window!")
but.bind("<Button-1>", btn_handler)
def btn_handler(e):
e.parent().destroy() # I need something like this!
# e.parent() ==>> main_window
# without writing class and defining self.parent ...
I am searching internet for several hours and I didn't find proper answer.
You can use
but.master
to access the parent of thebut
object.To get the container widget of a widget that's handling a callback, you can do:
That said, I'm not exactly sure why you're trying to avoid using a custom class. It's a natural solution to your problem.