Parthib Biswas Parthib Biswas - 7 months ago 157
Python Question

Changing color of a part of text of a Kivy widget

I'm trying to write a program where if I press a button, the color of a part of the text of a Label widget changes.

For example, there's a Label widget with text "1/0". Now, if I press the button, the color of 1 changes to some assigned color. This is the program I tried:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.utils import get_color_from_hex

Builder.load_string('''
#: import get_color_from_hex kivy.utils.get_color_from_hex
<b>:
orientation: 'horizontal'
Button:
text: 'Press Me'
on_press: num.text[0].color = get_color_from_hex('#04d3ff')

Label:
id: num
text: '1/0'
''')

class b(BoxLayout):
pass

class main(App):
def build(self):
return b()

if __name__ == "__main__":
main().run()


And this is the error I get:

Traceback (most recent call last):
File "b.py", line 28, in <module>
main().run()
File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 828, in run
runTouchApp()
File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 487, in runTouchApp
EventLoop.window.mainloop()
File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 619, in mainloop
self._mainloop()
File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 362, in _mainloop
EventLoop.idle()
File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 330, in idle
self.dispatch_input()
File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 315, in dispatch_input
post_dispatch_input(*pop(0))
File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 221, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7699)
File "/usr/lib/python2.7/dist-packages/kivy/core/window/__init__.py", line 1030, in on_motion
self.dispatch('on_touch_down', me)
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7699)
File "/usr/lib/python2.7/dist-packages/kivy/core/window/__init__.py", line 1046, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7699)
File "/usr/lib/python2.7/dist-packages/kivy/uix/widget.py", line 432, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7699)
File "/usr/lib/python2.7/dist-packages/kivy/uix/behaviors/button.py", line 110, in on_touch_down
self.dispatch('on_press')
File "kivy/_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7654)
File "kivy/_event.pyx", line 1224, in kivy._event.EventObservers.dispatch (kivy/_event.c:13497)
File "kivy/_event.pyx", line 1108, in kivy._event.EventObservers._dispatch (kivy/_event.c:12329)
File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1557, in custom_callback
exec(__kvlang__.co_value, idmap)
File "<string>", line 7, in <module>
AttributeError: 'str' object has no attribute 'color'


EDIT: SOLUTION

This edit is for those who happen to run into a similar situation and lands up here from a Google search. Inclement's solution worked perfectly. This is the solution code for the kv language:

<b>:
orientation: 'horizontal'
Button:
text: 'Press Me'
on_press: num.text = "[color=#04d3ff]1[/color]/0"
on_release: num.text = "[color=#ffffff]1[/color]/0"

Label:
id: num
markup: True
text: '1/0'

Answer

Use Kivy's basic markup syntax to set the colour, along with markup: True in the label.