Kivy RecycleBoxLayout更改字体颜色

2024-10-02 12:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直无法找到改变kivy recyclebox字体颜色的方法。如何更改标签属性?下面是我的代码

Python方面:

class ExampleViewer(RecycleView): 
    def __init__(self, **kwargs): 
        super(ExampleViewer, self).__init__(**kwargs) 
        self.data = [{'text': f"[color=[0,0,0,1]]{x}[/color]"} for x in range(20)]

基维方面:

ExampleViewer: 
    viewclass: 'Label'  # defines the viewtype for the data items. 
    orientation: "vertical"
  
    RecycleBoxLayout: 
        color: (0, 0, 0, 1)
        default_size: None, dp(56)
        markup: True 
  
        # defines the size of the widget in reference to width and height 
        default_size_hint: 1, None 
        size_hint_y: None
        height: self.minimum_height 
        orientation: 'vertical'

我尝试过为标签创建标记并直接更改颜色。这两种方法对我都不起作用。有办法改变吗


Tags: the方法inselfnonefordatasize
1条回答
网友
1楼 · 发布于 2024-10-02 12:32:08

更改标签小部件中文本颜色的方法是指定attibute,color

片段

self.data = [{'text': str(x), 'color': [1, 0, 1, 1]} for x in range(20)]

范例

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView

Builder.load_string('''
<RV>:
    viewclass: 'Label'
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
''')


class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x), 'color': [1, 0, 1, 1]} for x in range(100)]


class TestApp(App):
    def build(self):
        return RV()


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

输出

RecycleView - Label's text is pink colour

相关问题 更多 >

    热门问题