Home > Software design >  How to send value from loop tkinter label also destroy the whole frame?
How to send value from loop tkinter label also destroy the whole frame?

Time:11-22

I am creating a dynamic loop to create a button and I hope that I can create a frame that includes the delete button and label, when I click the delete button it can tell me the label text also delete the whole frame

def deleteEvent(num):
        print(num)
    for inx, num in enumerate (evid):
                print(num)
                f = tk.Frame(window)
                
                             
                #eventVar = tk.IntVar(f,value=evid[num])
                #eventVar.set(str(evid[x]))
                                
                e1=tk.Label(f, text='event content: ' cal.calevent_cget(num,option='text'))
                e1.pack(side='top')
                e2=tk.Label(f, text=num)
                e2.pack(side='top')
                #place(anchor="nw", x=0, y=0, width=0, height=0)
                #tk.Button(f, text='delete', command=lambda text=num: deleteEvent(text)).pack(side='top')
                e3=tk.Button(f, text='delete', command=lambda:[ (lambda num=num: deleteEvent(num)),f.destory]  )
                e3.pack(side='top')
                #delete_button=tk.Button(window, text='delete', command=deleteEvent).pack()
                #modify_button=tk.Button(window, text='modify', command=modifyEvent).pack()
                f.pack()
                print('end')
                #f.pack()

the terminal return error that

Traceback (most recent call last):
  File "C:\Users\CloudMosa\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "c:\Users\CloudMosa\Desktop\calander.py", line 52, in <lambda>
    e3=tk.Button(f, text='delete', command=lambda:[ (lambda num=num: deleteEvent(num)),f.destory]  )
AttributeError: 'Frame' object has no attribute 'destory'

CodePudding user response:

You have a typo, it is des- troy not des- tory

Also, change this line -

e3=tk.Button(f, text='delete', command=lambda:[ (lambda num=num: deleteEvent(num)),f.destroy()])

The () is needed when using lambda

CodePudding user response:

As the other answer points out that it is typo error and missing () when calling a function.

Apart from that, I would not recommend calling multiple functions in a lambda. Just do the required jobs inside single function, for example deleteEvent():

def deleteEvent(num, frame):
    print(num)
    frame.destroy()

for inx, num in enumerate (evid):
    print(num)
    f = tk.Frame(window)
    ...
    e3 = tk.Button(f, text='delete', command=lambda f=f, num=num: deleteEvent(num, f))
    ...
  • Related