我正在尝试制作一个自定义小部件,它有一个属性名称"MaxHeight",属性工作正常,但不在 KV 文件中。
我的自定义小部件 KV 代码是:
<ComboBox>
displaybox:displaybox
DropDown:
id: displaybox
height: root.height
而Python代码是:
class ComboBox(Button):
displaybox = ObjectProperty(None)
#displaybox = DropDown
Items = dict()
MaxHeight = NumericProperty(None)
def __init__(self, **kwargs):
super(ComboBox,self).__init__(**kwargs)
self.bind(MaxHeight=self.setter("MaxHeight"))
#self.displaybox.height=self.MaxHeight
#self.bind(self.displaybox.height=self.setter("MaxHeight"))
def AddItem(self,Title,Value):
if Value not in self.Items:
self.Items[Value] = Title
tmp = ComboBoxItem(text=Title,Tag=Value,
size_hint_y=None,height=32)
tmp.bind(on_release=lambda tmp: self.UpdateSelected(tmp.text, tmp.Tag))
self.displaybox.add_widget(tmp)
def UpdateSelected(self,Text, Value):
self.displaybox.select(Text)
self.text=Text
def on_release(self, *args, **kwargs):
self.displaybox.open(self)
def Dismiss(self):
self.displaybox.dismiss(self)
Code of how it is used:
Label:
text: 'Branch'
font_size: FS_B
text_size: self.size
ComboBox:
id: clientsdropdown
MaxHeight: 20
Button:
当应用启动时,错误显示为:
kivy.lang.ParserException: Parser: File "E:OS SupportworkspaceTestAppstartapp.kv", line 57:
...
55: ComboBox:
56: id: clientsdropdown
>> 57: MaxHeight: 20
58:
59: Button:
- 您能否建议/纠正我如何用 KV 语言定义自定义属性。
- 我正在尝试访问此最大高度并将其设置为 ComboBox 中的下拉控件,有没有办法将父小部件(组合框)高度访问到下拉列表控件。
编辑:在上面的代码中,我将所有"MaxHeight"重命名为"listheight",代码现在可以工作了,但是,当我添加以下代码时,下拉高度没有得到更新,您能否纠正我出错的地方。法典:
def on_listheight(self,instance,value):
self.displaybox.height=value
我按照建议使用 root.parent.height 达到了高度,但希望了解为什么它没有使用上面的属性更改代码进行更新,并且 Combobox Init 部分中的以下代码会抛出一个错误,说"属性错误:'NoneType' 对象没有属性'dismiss'",我怎样才能实现组合框在应用程序启动时被关闭,目前它在应用程序启动时显示为打开。谢谢。
self.displaybox.dismiss()
在kv语言中使用的属性应该以小写字母开头,因为kv使用它来区分它们与小部件。
有没有办法访问下拉控件的父小部件(组合框)高度。
我不完全理解这个问题,但一般来说,您可以使用 self.parent 访问父级。