I am making this program that deals with a lot of colors and it gives the user the freedom to change many of them. In one part of my program I use markup for one of my labels and and I realized something about the ‘color’ tag.
When my program starts I want my label to follow the theme but I get this warning when trying to set the color to the theme and it does not show the color correctly:
JavaScript
x
2
1
[WARNING] [ColorParser ] Invalid color format for 'app.hex_txt_color'
2
I don’t know how to have the hex code that is inside my py file work in the kv file. How can I solve this? My Code:
JavaScript
1
25
25
1
from kivy.lang import Builder
2
from kivy.utils import get_hex_from_color
3
from kivy.app import App
4
5
6
class Example(App):
7
txt_color = [1, 0, 0, 1]
8
hex_txt_color = get_hex_from_color(txt_color)[:-2]
9
10
def __init__(self, **kwargs):
11
super().__init__(**kwargs)
12
print(self.hex_txt_color)
13
self.kv = Builder.load_string('''
14
Label:
15
markup: True
16
text: "[color=app.hex_txt_color]I am red[/color] but not this!"
17
''')
18
19
def build(self):
20
return self.kv
21
22
23
if __name__ == "__main__":
24
Example().run()
25
Advertisement
Answer
loadString
interprets the entire value of text
as a string when enclosed by quotes. In order to make it interpret app.hex_txt_color
as a variable you can concatenate the variable to the string. As an example:
JavaScript
1
2
1
text: "[color="+ app.hex_txt_color +"]I am red[/color] but not this!"
2
You can use python code like:
JavaScript
1
2
1
text: "[color={}]I am red[/color] but not this!".format(app.hex_txt_color)
2