简体   繁体   English

更改 Kivy 小部件的部分文本的颜色

[英]Changing color of a part of text of a Kivy widget

I am trying to write a program where if I press a button, the color of a part of the text of a Label widget changes.我正在尝试编写一个程序,如果我按下一个按钮,Label 小部件的部分文本的颜色会发生变化。

For example, there's a Label widget with text "1/0".例如,有一个带有文本“1/0”的Label小部件。 Now, if I press the button, the color of 1 changes to some assigned color.现在,如果我按下按钮,1 的颜色会更改为某个指定的颜色。 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'

使用Kivy的基本标记语法来设置颜色以及markup: True标签中为markup: True

For the second question in your comment where you ask "I ran into another problem now. :\\ What if there is more than one Label? What if I want to change the first character of multiple Labels together?" 对于您评论中的第二个问题,您问“我现在遇到另一个问题。:\\如果有多个Label,该怎么办?如果我想一起更改多个Label的第一个字符,该怎么办?” You should open a new question and provide us with some code. 您应该打开一个新问题,并向我们提供一些代码。

However, at first glance it appears that you could simply loop through the labels and do the following: 但是,乍一看,您似乎可以简单地遍历标签并执行以下操作:

  1. Obtain the current text of the label (label.text) 获取标签的当前文本(label.text)

  2. Update the label color by using markup for the first character. 通过使用第一个字符的标记来更新标签颜色。 for example, if label.text = "my_text", I could highlight the "m" of the label text via updating the following inside the label: 例如,如果label.text =“ my_text”,则可以通过更新标签内的以下内容来突出显示标签文本的“ m”:

     text = '[color=FFFF00]'+label.text[0]+'[/color]'+label.text[1:] 

This answer assumes that you have a set of existing labels and you'd like to modify the first character of each. 该答案假设您有一组现有标签,并且您想要修改每个标签的第一个字符。 Without code, it is difficult to give a more specific answer but this general approach would work. 没有代码,很难给出更具体的答案,但是这种通用方法会起作用。

Inclement's solution worked perfectly. Inclement 的解决方案非常有效。 This is the solution code for the kv language:这是kv语言的解决方案代码:

<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'

This answer was posted as an edit to the question Changing color of a part of text of a Kivy widget by the OP Parthib Biswas under CC BY-SA 3.0.这个答案是作为对问题Changing color of a part of text of a Kivy widget编辑发布的,由 OP Parthib Biswas在 CC BY-SA 3.0 下发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM