You alredy use simple way,You can remember id of each button by my_btn = Button(master, ...)
, where my_btn
will be id of the button. Then collect them in the list and use for loop each time you want to call all buttons you added by their id. See:
import tkinter as tk
button_list = []
master = tk.Tk()
def change_color():
for button in button_list:
button.config(bg='red')
my_btn = tk.Button(master, text="Button 1")
my_btn.pack()
button_list.append(my_btn)
my_btn = tk.Button(master, text="Button 2")
my_btn.pack()
button_list.append(my_btn)
my_btn = tk.Button(master, text="Button 3")
my_btn.pack()
button_list.append(my_btn)
my_btn = tk.Button(master, text="CHANGE COLOR", command=change_color)
my_btn.pack()
master.mainloop()
You can use dictionary instead of list to choose button you need, it will be better practice.
Of course you can refer to all buttons like in the code above, but I do not think you will ever need it.
BTW you do not need to use is not None
, because if btn connected variable
already return you boolean so your loop should look like:
for btn in btns_list:
if btn connected variable:
btn.config(text='Press to reconfig', fg='green')
EDIT 1:
Example with dictionary instead of the list:
import tkinter as tk
# dictionary with buttons
d = {}
master = tk.Tk()
def change_color():
# for loop for dict keys
for button in d.keys():
# here we refer to each button(value of button key)
# and change the color
#
# here you can add some if statements to edit particular group
# of buttons
d[button].config(bg='red')
my_btn = tk.Button(master, text="Button 1")
my_btn.pack()
d['group1_btn_name1'] = my_btn
my_btn = tk.Button(master, text="Button 2")
my_btn.pack()
d['group2_btn_name2'] = my_btn
my_btn = tk.Button(master, text="Button 3")
my_btn.pack()
d['group1_btn_name3'] = my_btn
my_btn = tk.Button(master, text="CHANGE COLOR", command=change_color)
my_btn.pack()
master.mainloop()
But remember - each key in the dictionary should be unique, therefore button names (keys) should all be different (btn_name1, btn_name2, btn_name3
). You can group them by adding prefixes like some_group_btn1, some_group_btn2
... other_group_btnnnn...
You can create buttons and groups with any names, but something (for ex. _
) should divide group prefix and button name for defining particular group names in dictionary keys. For example you can add such if statement:
if 'group1' in button.split('_'):
d[button].config(bg='red')
It will modify buttons which refer to only group1
group. But remember that division sign _
or -
of you want should always be the same because of split operation.
- Check about dictionary keys here
- About split here