How to get the Label widget from tkinter?

Viewed 38

Here is my code:

import tkinter as tk

frame = tk.Tk()
lbl = tk.Label(frame, text=line)
lbl.bind("<Button>", lambda x=lbl, y=frame: delete_and_create(x, y))

this is the function:

def delete_and_create(wid, frame):
    wid.pack_forget()
    lbl = tk.Text(frame)
    lbl.pack()

When I try to do wid.pack_forget() I get an exception:

AttributeError: 'Event' object has no attribute 'pack_forget'

When I try to print wid this is what I get:

<ButtonPress event num=1 x=50 y=10>

Meaning, instead of transferring the Label widget (lbl) I'm transferring the event bind to the function delete_and_create

Is there a command for the Label widget in which I can get the widget?

1 Answers

As @Atlas435 said, tkinter passes an event object when it calls the function. Your lambda doesn't take that into account. That is why you need to change it to lambda event, x=lbl, y=frame: delete_and_create(x, y)

Related