Reputation: 49
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>]
Upvotes: 1
Views: 75
Reputation: 15508
As you are iterating over Autput
, so k
is each button and don't index:
for k in Autput1:
k.destroy()
If you are using range(len())
then it will work, because then you will be iterating using index:
for k in range(len(Autput1)):
Autput1[k].destroy()
Upvotes: 2