python - 如何在 kivy python 中更改树视图的背景颜色?
问题描述
我需要帮助来更改 kivy 上树视图的背景颜色。
我正在研究 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
为您喜欢的任何颜色。
您可以完全在 中执行此操作Python
,但您需要手动创建kv
自动为您创建的绑定:
list_label=TreeView(root_options=dict(text='My root label'),hide_root=False)
with list_label.canvas.before:
Color(1, 0, 0, 1)
self.background_rect = Rectangle()
list_label.bind(pos=self.adjust_rect_pos)
list_label.bind(size=self.adjust_rect_size)
def adjust_rect_size(self, treeview, new_size):
self.background_rect.size = new_size
def adjust_rect_pos(self, treeview, new_pos):
self.background_rect.pos = new_pos
推荐阅读
- ios - SwiftUI @Binding 初始化
- python - 用于计算文件哈希的异步编程
- python - ValueError:尝试相对导入超出顶级包(Scrapy)
- python - 为什么我会收到错误消息“Break outside Loop”?
- javascript - 16 位有符号整数的正则表达式
- javascript - 如何像另一个对象一样构建一个对象?
- wpf - 如何将字典键和值绑定到 Telerik RadPropertyGrid
- typescript - 从 promise 对象查询属性并保存在变量中
- python - 从 Excel 中提取实时数据以在我的 Python 模型中使用
- python - Python:Microsoft Exchange 通过 EWS 批准/拒绝电子邮件(使用 exchangelib)