繁体   English   中英

如何更改 kivy python 中树视图的背景颜色?

[英]How to change the background color of tree-view in kivy python?

我需要帮助来更改 kivy 上 treeview 的背景颜色。

我正在研究 python 中的 kivy 框架,该框架将列出一些标签。

但是在执行应用程序时会发生什么,我的应用程序背景颜色是白色,而树视图从应用程序背景中获取背景颜色。

下面是示例截图

在此处输入图像描述

示例代码:创建树视图。

list_label=TreeView(root_options=dict(text='My root label'),hide_root=False)
list_label.add_node(TreeViewLabel(text='My first item'))

将以下内容添加到您的.py

Builder.load_string('''
<TreeView>:
    canvas.before:
        Color:
            rgba: 1, 0, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size

''')

这会将背景更改为红色。 您可以将 1、0、0、1 替换1, 0, 0, 1您喜欢的任何颜色。

编辑:我有点回答了错误的问题,我下面的解决方案改变了覆盖背景的偶数/奇数节点的颜色。 我会把它留在这里以防万一。

原答案:

有几种方法可以给这只猫剥皮。 最简单的方法是使用TreeViewNode小部件的even_colorodd_color属性。 在您的情况下,您将如何使用它:

list_label=TreeView(root_options=dict(text='My root label'),hide_root=False)
list_label.add_node(TreeViewLabel(text='My first item', \
even_color=[0.5,0.1,0.1,1],odd_color=[0.1,0.1,0.5,1]))

创建自己的自定义小部件肯定会更,这同样容易:

from kivy.uix.treeview import TreeViewLabel
from kivy.uix.button import Button

class MyTreeViewLabel(Button, TreeViewLabel):
   def __init__(self, **kwargs):
      super(MyTreeViewLabel, self).__init__(**kwargs)
      self.even_color = [0.5,0.1,0.1,1]
      self.odd_color = [0.1,0.1,0.5,1]

然后,您的代码将是:

list_label=TreeView(root_options=dict(text='My root label'),hide_root=False)
list_label.add_node(MyTreeViewLabel(text='My first item'))

暂无
暂无

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

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