[英]How to change the background color of tree-view in kivy python?
將以下內容添加到您的.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_color
和odd_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.