簡體   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