Kivy嵌套ID

时间:2015-10-26 19:10:24

标签: python user-interface kivy

我正在尝试创建客户管理软件,因此我需要创建一个user_id。我之所以选择GUI,是因为它是开源和Kivy

这个软件有多个面板,所以我需要LGPL来访问每个面板中的小部件。我用kv语言创建了ID个规则,但是当我将另一个类嵌套时,我无法访问Kivy。下面是一个示例代码:

LayoutTestApp.py

ID

njord.kv

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()

当我启动软件时,打印仅返回#: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 {}吗? 谢谢你。

1 个答案:

答案 0 :(得分:1)

您没有看到ID的原因是您正在TestPanel构造函数中打印。它还没有完成创建,更不用说添加了任何东西。如果在创建GUI之后打印id(即按下按钮),那么您将看到ID:

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>>}