'Changing the background colour of loop generated entry boxes based on user input in tkinter

I have the following code where I use a for loop to create some entry boxes.

# loop to create labels and entry boxes for main table
for i in range(int(self.number_of_indicators)): # columns
    for j in range(int(self.project_length_entry.get())): # rows

        # creating labels, using list of indicator names
        indicator_target_label = Label(frame, text=str(self.indicator_names[i].get()))
        # create entry box
        self.indicator_month_entry = Entry(frame)

        # adding labels to screen
        indicator_target_label.grid(row=100, column=3+i)
        # add entry box to screen
        self.indicator_month_entry.grid(row=101 + j, column=3+i)
    # end for loop
# end for loop

I want to be able to change the background colour of the boxes to one of three colours based on the user input (in the boxes where there is user input) after clicking the 'confirm button' - this input will be an integer number.

I have created the button with the following code:

# create button
sf_button = Button(frame, text='Confirm', command=lambda: sf_button)
# add button to screen
sf_button.grid(row=200, column=2)

I have tried using the following code for the button function (the numbers at the moment are arbitrary :

def sf_button():
    if float(self.indicator_month_entry.get()) == 2:
        self.indicator_month_entry.configure(bg='yellow')
    elif float(self.indicator_month_entry.get()) > 3:
        self.indicator_month_entry.configure(bg='blue')
    else:
        self.indicator_month_entry.config(bg='red')

This runs fine, but doesn't do what I want.

Do I need to assign the entry boxes to a list and address them that way? How will that account for n number of boxes?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source