【发布时间】:2015-10-27 04:46:53
【问题描述】:
我正在尝试创建一个客户管理软件,所以我需要创建一个GUI。我选择了Kivy,因为它是开源的,而LGPL。
这个软件有多个面板,所以我需要ID 来访问每个面板中的小部件。我用kv语言创建了Kivy规则,但是当我嵌套一个类是另一个类时,我无法访问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:'\/'
当我启动软件时,打印只返回{}。
例如,有人可以告诉我如何访问 button_up ID 吗?
提前谢谢。
【问题讨论】:
标签: python user-interface kivy