0
for bear in self.bears:
                 button=tk.Button(self,text=polar.spec(),command=lambda 
                          id=polar.spec:self.polar_ad(id))
                 button.grid(row=_r,column=_c,sticky=W+E+N+S)
                 _row+=1

Hello, so i've created a number of buttons in a for loop, but when i click the button, i want to be able to disable it. If the buttons were created individually then i could use

button["state"]="disabled"

but how can i use this by using the id of the button? Thank you

loaf
  • 5
  • 2
  • Does this answer your question? [How to access one Button in a grid of Button](https://stackoverflow.com/a/59073175/7414759) – stovfl Mar 04 '20 at 20:33

2 Answers2

1

I would add the buttons to an array upon creation, then loop through the array and check ids.

buttons = []
for bear in self.bears:
   btn = tk.Button(self,text=polar.spec(),command=lambda id=polar.spec:self.polar_ad(id))
   btn.grid(row=_r,column=_c,sticky=W+E+N+S)
   buttons.append(btn)
   _id += 1
   _row += 1

Then check button IDs

for b in buttons:
   if b.id == <id_of_button_press>:
       b['state'] = "disabled"

I'm not sure if you can get a button's id this way, but the principal should make sense.

Uuuuuumm
  • 608
  • 5
  • 21
0
buttons=[]
num=0
for bear in self.bears:
                 button=tk.Button(self,text=polar.spec(),command=lambda 
                         id=polar.spec,index=num:self.polar_ad(id,index))
                 button.grid(row=_r,column=_c,sticky=W+E+N+S)
                 num+=1
                 _row+=1

then

self.polar(id,index):
                 buttons[index]["state"]="disabled"

thank for your idea really helped me get on the right tracks @Uuuuuumm

loaf
  • 5
  • 2