My question is for example i have 3 buttons (bt1,bt2,bt3). How can i simultaneously change their state without calling them by their proper names, using btn[i] or something?
-
3why not put them in a list and iterate over?Padraic Cunningham– Padraic Cunningham2014-10-25 21:44:32 +00:00Commented Oct 25, 2014 at 21:44
Add a comment
|
1 Answer
You can use a for loop like this, which just takes all buttons and disables them in an easy to write way:
for x in (btn1, btn2, btn3):
x.config(state = 'disabled')
I think this does what you want it to?
here is a full example:
import tkinter as tk
r = tk.Tk()
def disable_all():
d.destroy()
for z in (a, b, c):
z.config(state = 'disabled')
def func(y):
print('you clicked button ', y)
a = tk.Button(text = 'A', command = lambda: func('a'))
b = tk.Button(text = 'B', command = lambda: func('b'))
c = tk.Button(text = 'C', command = lambda: func('c'))
d = tk.Button(text = 'disable all', command = disable_all)
for x in (a, b, c, d):
x.pack()
r.mainloop()
hope this helps!