来自 ids 的 Kivy 回调自定义小部件调用引发 AttributeError
Kivy callback custom widget call from ids raise AttributeError
我正在尝试从其 ID 调用自定义小部件方法。
但是我收到了 AttributeError: 'LabelBox' object has no attribute 'change_first_text'
。
可以在 PanelApp.py 文件中找到尽可能简单的工作示例:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_file("panel.kv")
class LabelBox(BoxLayout):
def __init__(self, *args, **kwargs):
super(LabelBox, self).__init__(*args, **kwargs)
def change_first_text(self, text):
self.ids.first.text = text
class ButtonList(BoxLayout):
pass
class Test(TabbedPanel):
pass
class TabbedPanelApp(App):
def build(self):
self.test = Test()
self.btn_list = ButtonList()
self.vbox = BoxLayout(orientation="vertical")
self.vbox.add_widget(self.btn_list)
self.vbox.add_widget(self.test)
return self.vbox
def change_first(self, value):
print("Button clicked and new value is: '{}'".format(value))
self.test.ids.lbs.change_first_text(value)
if __name__ == '__main__':
TabbedPanelApp().run()
和 panel.kv 文件:
<ButtonList@ButtonList>:
orientation: "horizontal"
Button:
text: "change fisrt to me"
on_press: app.change_first(self.text)
Button:
text: "change two to me"
<LabelBox@BoxLayout>:
Label:
id: first
text: "first"
Label:
id: two
text: "two"
<Test>:
size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'first tab'
LabelBox:
id: lbs
调用脚本导致我无法理解的运行时错误。
您知道如何通过应用程序管理此类事件回调吗?
你的问题是你正在创建 2 个 classes 叫做 LabelBox
:
1.
class LabelBox(BoxLayout):
def __init__(self, *args, **kwargs):
super(LabelBox, self).__init__(*args, **kwargs)
def change_first_text(self, text):
self.ids.first.text = text
2.
<LabelBox@BoxLayout>:
Label:
id: first
text: "first"
Label:
id: two
text: "two"
我知道你只想有一个 class 所以在 .py 中创建继承是合适的,并且只在 [=14= 中实现 children ].解决方法是在.kv
中改delete @BoxLayout
<LabelBox>:
Label:
id: first
text: "first"
Label:
id: two
text: "two"
我正在尝试从其 ID 调用自定义小部件方法。
但是我收到了 AttributeError: 'LabelBox' object has no attribute 'change_first_text'
。
可以在 PanelApp.py 文件中找到尽可能简单的工作示例:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_file("panel.kv")
class LabelBox(BoxLayout):
def __init__(self, *args, **kwargs):
super(LabelBox, self).__init__(*args, **kwargs)
def change_first_text(self, text):
self.ids.first.text = text
class ButtonList(BoxLayout):
pass
class Test(TabbedPanel):
pass
class TabbedPanelApp(App):
def build(self):
self.test = Test()
self.btn_list = ButtonList()
self.vbox = BoxLayout(orientation="vertical")
self.vbox.add_widget(self.btn_list)
self.vbox.add_widget(self.test)
return self.vbox
def change_first(self, value):
print("Button clicked and new value is: '{}'".format(value))
self.test.ids.lbs.change_first_text(value)
if __name__ == '__main__':
TabbedPanelApp().run()
和 panel.kv 文件:
<ButtonList@ButtonList>:
orientation: "horizontal"
Button:
text: "change fisrt to me"
on_press: app.change_first(self.text)
Button:
text: "change two to me"
<LabelBox@BoxLayout>:
Label:
id: first
text: "first"
Label:
id: two
text: "two"
<Test>:
size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'first tab'
LabelBox:
id: lbs
调用脚本导致我无法理解的运行时错误。 您知道如何通过应用程序管理此类事件回调吗?
你的问题是你正在创建 2 个 classes 叫做 LabelBox
:
1.
class LabelBox(BoxLayout):
def __init__(self, *args, **kwargs):
super(LabelBox, self).__init__(*args, **kwargs)
def change_first_text(self, text):
self.ids.first.text = text
2.
<LabelBox@BoxLayout>:
Label:
id: first
text: "first"
Label:
id: two
text: "two"
我知道你只想有一个 class 所以在 .py 中创建继承是合适的,并且只在 [=14= 中实现 children ].解决方法是在.kv
@BoxLayout
<LabelBox>:
Label:
id: first
text: "first"
Label:
id: two
text: "two"