I have a large list of names and scores from a survey. I am hoping there is a simple way to loop through the list and color the score red or green based on its value. I have successfully been able to color one of the values but my solution requires a lot of lines and I am hoping there is a quicker way to accomplish this other than copy and pasting the if elif else statements after each "_data" line, here is a snippet of the code:
`
one_frame['text'] = score.index[0]
one_data['text'] = score.iloc[0, 0]
if c_metric == 'Sleep' and score.iloc[0, 0] < 5 or c_metric != 'Sleep' and score.iloc[0, 0] <2.5:
one_data.configure(fg='red')
elif c_metric == 'Sleep' and score.iloc[0, 0] > 5 or c_metric != 'Sleep' and score.iloc[0, 0] > 4:
one_data.configure(fg='green')
else:
one_data.configure(fg='white')
two_frame['text'] = score.index[1]
two_data['text'] = score.iloc[1, 0]
three_frame['text'] = score.index[2]
three_data['text'] = score.iloc[2, 0]
four_frame['text'] = score.index[3]
four_data['text'] = score.iloc[3, 0]
`
The data frame is 2 columns, a name and a score.
I have tried a few combination of a for loop but have not found the right solution to color the text appropriately
CodePudding user response:
I realize this may be a unique issue but if anyone comes across a similar problem I have found a solution after a lot of trial and error:
I have added the score.iloc to a list, then under a new function looped through the list and colored as needed:
global avgs
avgs = []
try:
one_frame['text'] = score.index[0]
one_data['text'] = score.iloc[0, 0]
avgs.append(score.iloc[0, 0])
two_frame['text'] = score.index[1]
two_data['text'] = score.iloc[1, 0]
avgs.append(score.iloc[1, 0])
...
for i, k, g in zip(avgs, labs, fras):
if c_metric == 'Sleep' and i < 6 or c_metric == 'RPE' and i > 15 \
or c_metric != 'Sleep' and c_metric != 'RPE' and i < 2.5:
k.configure(fg='red')
g.configure(fg='red')
elif c_metric == 'Sleep' and i > 7 or c_metric == 'RPE' and i < 11 \
or c_metric != 'Sleep' and c_metric != 'RPE' and i > 4:
k.configure(fg='green')
g.configure(fg='green')
else:
k.configure(fg='white')
I also realize this is not the full code so if you do have any more questions I can explain a bit more and share the full code.
Good Luck!