Home > Mobile >  Changing the Label Color in kivy if an int is bigger than 22
Changing the Label Color in kivy if an int is bigger than 22

Time:02-23

So I want to change the color of an label in kivy if some variable is bigger than 22 I tried everything, this is how it should work. like if ZZ is bigger than 22 the label color is red and if it is lower the label color is green, in the code example i clearfy it as an str with the number 40 Here my code hope you can help me.

import kivy
import requests
import json
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.properties import ListProperty

Builder.load_string("""
<MySec>:
    rgba1: (1,.2,.2,.2)
    GridLayout:
        cols: 1
        size: root.width,root.height
        GridLayout:
            Label:
                id: kv_sec1
                text: root.string1
                font_size: 30
                canvas.before:
                    Color:
                        rgba: root.rgba1
""")


class MySec(BoxLayout):
    string1 = StringProperty('')


class MyApp(App):
    def build(self):
        Clock.schedule_interval(lambda dt: self.update_time(), 0)
        return MySec()

    def update_time(self):
        global ZZ
        ZZ = 40 #"minimal reproducible example"
        self.root.string1 = str(ZZ)


    def Temp(self, instance):
        ZZ = int(self.ZZ)
        if ZZ > 22:
            self.rgba4 = [0.,1.,0.,1.] #gruen
        else:
            self.rgba4 = [1.,0.,0.,1.] #root


if __name__ == '__main__':
    MyApp().run()

CodePudding user response:

If I understood your problem properly then you want to change the text colour (color) depending on some value ( of ZZ perhaps).

To achieve that, first create a NumericProperty (assuming ZZ takes only number) for ZZ in MyApp. With that you will be able to use it everywhere in your code.

Now if the label's text is just this value of ZZ set it as text: str(app.ZZ). Now bind its color prop. depending on this value. One way of doing that would be simply like color: [0.,1.,0.,1.] if app.ZZ > 22 else [1.,0.,0.,1.].

With some more changes your code should now look like,

import kivy
import requests
import json
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import (
    StringProperty,
    NumericProperty,
    ListProperty
)

Builder.load_string("""
<MySec>:
    rgba1: (1,.2,.2,.2)
    GridLayout:
        cols: 1
        size: root.width,root.height
        GridLayout:
            Label:
                id: kv_sec1
                text: str(app.ZZ)
                font_size: 30
                color: [0.,1.,0.,1.] if app.ZZ > 22 else [1.,0.,0.,1.]

""")


class MySec(BoxLayout):
    string1 = StringProperty('')


class MyApp(App):
    ZZ = NumericProperty(1)

    def build(self):
        Clock.schedule_interval(self.update_time, 0.5)
        return MySec()

    def update_time(self, *args):
        self.ZZ *= -2


    def Temp(self, instance):
        ZZ = int(self.ZZ)
        if ZZ > 22:
            self.rgba4 = [0.,1.,0.,1.] #gruen
        else:
            self.rgba4 = [1.,0.,0.,1.] #root


if __name__ == '__main__':
    MyApp().run()

Note:

Here, method Temp has not been used. There are definitely other ways to accomplish that though.

  • Related