[Python] How can I change my variable my_color if the function on_button_click is triggered

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    Im couple of days into python and I cant find the propper solution for my problem. I read global variable is not the best choice so I want to figure out the propper way. I want to change the variable my_color always if the function on_button_click is triggerd. Its giving me a new color if on_button_click is triggerd, but its not updating the my_color variable. I guess it as something to do with mutable and immutable objects maybe? How can I solve it?

    class WidgetsExample(BoxLayout):
    #random change in Text "color"

    color_list = [[0,0,1,1],[1,0,0,1],[1,1,0,1]]
    font_color = random.choice(color_list)

    #color in word
    colors = ["RED", "BLUE", "YELLOW", "GREEN"]
    my_color = (StringProperty(""))

    #sum of score
    score = []
    my_score = StringProperty("Punkte: ")

    def on_button_click(self):
    self.my_color = random.choice(self.colors)
    self.score.append(5)
    font_color = (random.choice(self.color_list))
    print(font_color)
    return self.font_color
    print((sum(self.score)))
    self.my_score = "Punkte: " + str(sum(self.score))

    Continue reading...

Compartilhe esta Página