Skip to content Skip to sidebar Skip to footer

Tkinter: Changing Button Attributes For Buttons Created In Loop

Right now this would work, but I would prefer to do it with for loops and a list. I've tried, but I cannot figure out how to get the name of the buttons that were created in the fo

Solution 1:

Don't try to create variable names, just store the buttons in a list:

buttons = []
for step in STEPS:
    buttons.append(tk.Button(...))
    ...

...
buttons[i].configure(...)

Solution 2:

Just as a side note, you were dead on Bryan, thank you. This is the working code:

import tkinter as tk

def toggle(num):
    if buttons[num].config('relief')[-1] == 'sunken':
      buttons[num].config(relief='raised')
    else:
      buttons[num].config(relief='sunken')

root = tk.Tk()

root.title("Chamber Calibration")

#------------------------------------------
# Sets up the different temperature buttons
#------------------------------------------

STEPS = ['-65C', '-40C', '-20C', '0C', '23C', '30C', '45C', '65C', '90C', '130C', '165C',
         '38C 15%RH', '38C 95%RH', '50C 95%RH', '85C 95%RH']

buttons = []
count = 0
for step in STEPS:
    buttons.append(tk.Button(root, text=step, width = 25, relief='raised',
                        command=lambda num=count: toggle(num)))
    buttons[count].grid(row = count, column = 3)
    count += 1

#-----------
# Starts GUI
#-----------
root.mainloop()

Post a Comment for "Tkinter: Changing Button Attributes For Buttons Created In Loop"