How can I destroy the buttons generated using a for loop (after been clicked)
Question:
I already generated the buttons which their name is based on the list output1.
So, This program will create 4 buttons respectively from ‘key1’ to ‘key4’
, whenever I click the button key1, the search box B will be replaced by "key1"
,similarly, button key 2,key3,key4 will replace "key2","key3","key4" respectively in search box B…
My objective is once I click one of these buttons, It will insert the word in the seach box B, and these 4 buttons will automatically be deleted
root = Tk()
def setTextInput(text):
B.delete(0,"end")
B.insert(0, text)
for k in Autput1:
Autput1[k].destroy()
j=0
if j==0:
output1 = ['key1','key2','key3','key4']
global Autput1
Autput1 = []
k=0
for e in output1:
Autput1.append(Button(root,text=e,command=lambda e=e:setTextInput(str(e))))
Autput1[k].grid()
print(Autput1)
k+=1
A = Label(root,text="Search your song here",font=('Roboto',10),bg='#c4302b')
A.grid(row=0,column=0,columnspan=2,padx=10,pady=10)
B = Entry(root,font=('Roboto',20))
B.grid(row=1,column=0,columnspan=2,padx=10,pady=10)
C= Button(root,image=img,command=getsong)
C.grid(row=1,column=2,padx=10,pady=10)
root.mainloop()
This is the part of how I try to destroy the button which appends inside the list Autput1.
for k in Autput1:
Autput1[k].destroy()
and This is the error and the only problem I try to fix
TypeError: list indices must be integers or slices, not Button
You might be wondering what is the value Autput1
[<tkinter.Button object .!button2>, <tkinter.Button object .!button3>, <tkinter.Button object
.!button4>, <tkinter.Button object .!button5>]
Answers:
In order to make buttons in a loop you need to either use partial for the command:
from functools import partial
# ...
for e in output1:
Autput1.append(Button(root,text=e,command=partial(setTextInput, e)))
Or bind the data to lambda with a default argument:
for e in output1:
Autput1.append(Button(root,text=e,command=lambda data=str(e):setTextInput(data)))
My problem is now solved! I just changed this line.
Thank you to Novel!
for e in output1:
Autput1.append(Button(root,text=e,command=lambda data=str(e):setTextInput(data)))
Also, ‘change lambda to lambda e=e’ also helped solving my problem
for e in output1:
Autput1.append(Button(root,text=e,command=lambda e=e:setTextInput(str(e))))
I already generated the buttons which their name is based on the list output1.
So, This program will create 4 buttons respectively from ‘key1’ to ‘key4’
, whenever I click the button key1, the search box B will be replaced by "key1"
,similarly, button key 2,key3,key4 will replace "key2","key3","key4" respectively in search box B…
My objective is once I click one of these buttons, It will insert the word in the seach box B, and these 4 buttons will automatically be deleted
root = Tk()
def setTextInput(text):
B.delete(0,"end")
B.insert(0, text)
for k in Autput1:
Autput1[k].destroy()
j=0
if j==0:
output1 = ['key1','key2','key3','key4']
global Autput1
Autput1 = []
k=0
for e in output1:
Autput1.append(Button(root,text=e,command=lambda e=e:setTextInput(str(e))))
Autput1[k].grid()
print(Autput1)
k+=1
A = Label(root,text="Search your song here",font=('Roboto',10),bg='#c4302b')
A.grid(row=0,column=0,columnspan=2,padx=10,pady=10)
B = Entry(root,font=('Roboto',20))
B.grid(row=1,column=0,columnspan=2,padx=10,pady=10)
C= Button(root,image=img,command=getsong)
C.grid(row=1,column=2,padx=10,pady=10)
root.mainloop()
This is the part of how I try to destroy the button which appends inside the list Autput1.
for k in Autput1:
Autput1[k].destroy()
and This is the error and the only problem I try to fix
TypeError: list indices must be integers or slices, not Button
You might be wondering what is the value Autput1
[<tkinter.Button object .!button2>, <tkinter.Button object .!button3>, <tkinter.Button object
.!button4>, <tkinter.Button object .!button5>]
In order to make buttons in a loop you need to either use partial for the command:
from functools import partial
# ...
for e in output1:
Autput1.append(Button(root,text=e,command=partial(setTextInput, e)))
Or bind the data to lambda with a default argument:
for e in output1:
Autput1.append(Button(root,text=e,command=lambda data=str(e):setTextInput(data)))
My problem is now solved! I just changed this line.
Thank you to Novel!
for e in output1:
Autput1.append(Button(root,text=e,command=lambda data=str(e):setTextInput(data)))
Also, ‘change lambda to lambda e=e’ also helped solving my problem
for e in output1:
Autput1.append(Button(root,text=e,command=lambda e=e:setTextInput(str(e))))