问题描述
我正在尝试创建一个自定义TreeViewNode来与TreeView一起使用.只是想要一张带有标签的图像,仅此而已.
I'm attempting to create a custom TreeViewNode to use with a TreeView.Just want an image with a label next to it, nothing more.
然而,尝试创建自定义节点...甚至在源中复制它的操作也不会产生相同的结果.
Yet attempting to create a custom node... even replicating how it's done in the source doesn't yield the same results.
TreeViewlabel实际上只是一个带有注释的类声明
TreeViewlabel is literally just a class declaration with a comment in it
class TreeViewLabel(Label, TreeViewNode):
'''there's just a comment here'''
这是我用于比较的TreeViewLabel版本
This is my version of the TreeViewLabel for comparison
class TreeViewImageLabel(Label, TreeViewNode):
"""oh boy this is a comment"""
尝试创建此代码的复本并将其添加到树中会产生以下结果.我的版本是第一个标签,后续的标签是默认的TreeViewLabel
Well attempting to create a carbon copy of this code and adding it to a tree yields the following. My version is the first label, and the subsequent labels are the default TreeViewLabel
全部隔开,未对齐且巨大.没有kivy附带的TreeViewLabel整洁.
It's all spaced out, un-aligned and huge. Nothing as neat and tidy as the TreeViewLabel that comes with kivy.
到底发生了什么?它不仅看上去不像TreeViewLabel,而且尝试用size,size_hints,pos等配置小部件,也不会从该标签相对于树的位置动摇标签.
What the heck is going on? Not only does it look nothing like the TreeViewLabel, but attempting to configure the widget with size, size_hints, pos, etc. does nothing to budge that label from where it is relative to the tree.
我最初的计划是让我的节点从具有图像和标签的boxlayout继承,但是随着自定义节点的大小确定,鉴于我现在所知道的,不可能获得任何看起来像TreeViewNode的东西.
My original plan was to have my node inherit from a boxlayout with an image and label stuck in it, but with the sizing of custom nodes it's impossible to get anything that looks like a TreeViewNode given what I know right now.
因此这是实施修补程序后的样子:
So this is how it looks with the fix implemented:
对于其他想要像我一样制作带有图片和标签的TreeViewNode的人,这是使它起作用的代码:
For anyone else looking to make a TreeViewNode with a picture and label as I've done, this is the code that got it working:
#python
class TreeViewImageLabel(BoxLayout, TreeViewNode):
pass
.kv
#kivy language
<TreeViewImageLabel>:
height: max(lbl.texture_size[1] + dp(10), dp(24))
Image:
size: (max(lbl.texture_size[1] + dp(10), dp(24)), max(lbl.texture_size[1] + dp(10), dp(24)))
size_hint: (.05, 1)
id:img
source: "smiley.png"
Label:
size_hint: (.9, 1)
id:lbl
text_size: self.width, None
text: "test"
推荐答案
您仅复制了TreeViewLabel
类的一部分.您重新创建了Python类,但是还有一个 kv规则:
You've only copied part of the TreeViewLabel
class. You recreated the Python class, but there is also a kv rule which is applied as well:
<TreeViewLabel>:
width: self.texture_size[0]
height: max(self.texture_size[1] + dp(10), dp(24))
text_size: self.width, None
这篇关于Kivy制作自定义TreeViewNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!