绝望的嵌套 ID
Kivy nested IDs
我正在尝试创建一个客户管理软件,所以我需要创建一个 GUI
。我选择 Kivy
因为它是开源的并且 LGPL
.
这个软件应该有多个面板,所以我需要 ID
来访问每个面板中的小部件。我用 kv 语言创建了 Kivy
规则,但是当我嵌套 class 是另一个时,我无法访问 ID
。下面是一个示例代码:
LayoutTestApp.py :
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
class SkipperList(GridLayout):
pass
class TestPanel(BoxLayout):
def __init__(self, **kwargs):
super(TestPanel, self).__init__(**kwargs)
print "TestPanel ids:", self.ids
class MasterPanel(TabbedPanel):
pass
class NjordApp(App):
def __init__(self, **kwargs):
super(NjordApp, self).__init__(**kwargs)
def build(self):
root = MasterPanel()
return root
if __name__ == '__main__':
application = NjordApp()
application.run()
njord.kv
#:kivy 1.9.0
<MasterPanel>
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'Skippers'
BoxLayout:
padding: 10
spacing: 10
TestPanel:
<TestPanel>:
id: SkipperPanelId
BoxLayout:
padding: 10
spacing: 10
BoxLayout:
orientation: 'vertical'
Label:
text: 'List des mecs'
size_hint: 1, 0.09
Button:
id: button_up
size_hint: 1, 0.08
text:'/\'
Button:
id: button_down
size_hint: 1, 0.08
text:'\/'
当我启动软件时,只打印 return {}
。
例如,有人可以告诉我如何访问 button_up ID
吗?
提前致谢。
您看不到 ID 的原因是您在 TestPanel
的 构造函数 中打印。它还没有完成创建,更不用说添加任何东西了。如果在创建 GUI 后打印 id(即,通过按下按钮),那么您将看到 ids:
class TestPanel(BoxLayout):
def __init__(self, **kwargs):
super(TestPanel, self).__init__(**kwargs)
print "TestPanel ids:", self.ids
def test(self, *x):
print self.ids
...
Button:
id: button_up
size_hint: 1, 0.08
text:'/\'
on_press: root.test()
输出:
{'button_down': <WeakProxy to <kivy.uix.button.Button object at 0x7f63159052c0>>, 'button_up': <WeakProxy to <kivy.uix.button.Button object at 0x7f63158f3738>>}
我正在尝试创建一个客户管理软件,所以我需要创建一个 GUI
。我选择 Kivy
因为它是开源的并且 LGPL
.
这个软件应该有多个面板,所以我需要 ID
来访问每个面板中的小部件。我用 kv 语言创建了 Kivy
规则,但是当我嵌套 class 是另一个时,我无法访问 ID
。下面是一个示例代码:
LayoutTestApp.py :
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
class SkipperList(GridLayout):
pass
class TestPanel(BoxLayout):
def __init__(self, **kwargs):
super(TestPanel, self).__init__(**kwargs)
print "TestPanel ids:", self.ids
class MasterPanel(TabbedPanel):
pass
class NjordApp(App):
def __init__(self, **kwargs):
super(NjordApp, self).__init__(**kwargs)
def build(self):
root = MasterPanel()
return root
if __name__ == '__main__':
application = NjordApp()
application.run()
njord.kv
#:kivy 1.9.0
<MasterPanel>
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'Skippers'
BoxLayout:
padding: 10
spacing: 10
TestPanel:
<TestPanel>:
id: SkipperPanelId
BoxLayout:
padding: 10
spacing: 10
BoxLayout:
orientation: 'vertical'
Label:
text: 'List des mecs'
size_hint: 1, 0.09
Button:
id: button_up
size_hint: 1, 0.08
text:'/\'
Button:
id: button_down
size_hint: 1, 0.08
text:'\/'
当我启动软件时,只打印 return {}
。
例如,有人可以告诉我如何访问 button_up ID
吗?
提前致谢。
您看不到 ID 的原因是您在 TestPanel
的 构造函数 中打印。它还没有完成创建,更不用说添加任何东西了。如果在创建 GUI 后打印 id(即,通过按下按钮),那么您将看到 ids:
class TestPanel(BoxLayout):
def __init__(self, **kwargs):
super(TestPanel, self).__init__(**kwargs)
print "TestPanel ids:", self.ids
def test(self, *x):
print self.ids
...
Button:
id: button_up
size_hint: 1, 0.08
text:'/\'
on_press: root.test()
输出:
{'button_down': <WeakProxy to <kivy.uix.button.Button object at 0x7f63159052c0>>, 'button_up': <WeakProxy to <kivy.uix.button.Button object at 0x7f63158f3738>>}