简体   繁体   English

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

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

I need help in changing the background color of the treeview on kivy.我需要帮助来更改 kivy 上 treeview 的背景颜色。

I am working on the kivy framework in python that will list some labels.我正在研究 python 中的 kivy 框架,该框架将列出一些标签。

But what happens while executing the application is, my apps background color is white and the tree-view gets the background-color from the application background.但是在执行应用程序时会发生什么,我的应用程序背景颜色是白色,而树视图从应用程序背景中获取背景颜色。

Below is the sample-screenshot下面是示例截图

在此处输入图像描述

Sample Code: To create tree view.示例代码:创建树视图。

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

Add the following to your .py :将以下内容添加到您的.py

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

''')

This changes the background to red.这会将背景更改为红色。 You can replace 1, 0, 0, 1 with any color you prefer.您可以将 1、0、0、1 替换1, 0, 0, 1您喜欢的任何颜色。

EDIT: I kind of answered the wrong question, my solution below changes the color of the even/odd nodes which overlay the background.编辑:我有点回答了错误的问题,我下面的解决方案改变了覆盖背景的偶数/奇数节点的颜色。 I'll leave it here in case it's helpful.我会把它留在这里以防万一。

Original Answer:原答案:

There are a couple ways to skin this cat.有几种方法可以给这只猫剥皮。 The easiest is to use the even_color and odd_color properties of the TreeViewNode widget.最简单的方法是使用TreeViewNode小部件的even_colorodd_color属性。 Here's how you would use that in your case:在您的情况下,您将如何使用它:

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]))

It would definitely be more DRY to create your own custom widget which is equally easy:创建自己的自定义小部件肯定会更,这同样容易:

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]

Then, your code would just be:然后,您的代码将是:

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