如何更改父类实例的属性

时间:2016-02-04 09:14:04

标签: python kivy

我尝试使用kivy制作自定义布局,我需要使用滚动视图按钮生成一个gridlayout并将其放在另一个布局中。

每次我点击生成按钮的按钮,它都会按下"获取链接"按钮,如果我再次点击它而不是向现有的gridlayout添加按钮,它会创建一个新的gridlayout。

这是我按下“获取链接”之前的应用程序。按键 before I press the button

我第一次按下'获取链接'按键 First time the button is pressed

第二次按下按钮 enter image description here

class RootWidget(BoxLayout):
    pass

class Scrollbox(BoxLayout):
    def __init__(self, **kwargs):
        super(Scrollbox, self).__init__(**kwargs)
        self.orientation = 'vertical'

class CustomLayout(FloatLayout):

    def __init__(self, **kwargs):
        # make sure we aren't overriding any important functionality
        super(CustomLayout, self).__init__(**kwargs)

        with self.canvas.before:
            Color(0, 1, 0, 1)  # green; colors range from 0-1 instead of 0-255
            self.rect = Rectangle(size=self.size, pos=self.pos)

        self.bind(size=self._update_rect, pos=self._update_rect)

    def _update_rect(self, instance, value):
        self.rect.pos = instance.pos
        self.rect.size = instance.size

class MainApp(App):

    link_buttons = 0

    def build(self):
        root = RootWidget()
        c = CustomLayout()
        s = Scrollbox()
        root.add_widget(c)
        root.add_widget(s)

        def on_enter(self):
            func = Function()
            buttons = func.buttons()
            s.add_widget(buttons)

        get_buttons = Button(
            text='Get links',
            size_hint=(1, 0),
            pos=(20, 20))
        s.add_widget(get_buttons)

        get_buttons.bind(on_press=on_enter)
        return root

class Function(MainApp):

    def buttons(self):
        if self.link_buttons == 0:
            layout = GridLayout(cols=1, padding=1, spacing=10,
                    size_hint=(None, None), width=10)
            layout.bind(minimum_height=layout.setter('height'))
            self.link_buttons += 1

        for buttn in range(20):
            btn = Button(text='test', size=(200, 50),
                     size_hint=(None, None))
            try:
                self.layout.add_widget(btn)
            except:
                layout.add_widget(btn)

        # create a scroll view, with a size < size of the grid
        root = ScrollView(size_hint=(None, None), size=(200, 400),
             pos_hint={'center_x': .5, 'center_y': .5}, do_scroll_x=False)
        root.add_widget(layout)
        return root

if __name__=='__main__':
    MainApp().run()

1 个答案:

答案 0 :(得分:1)

你有一些问题:

1)来自MainApp的功能内容 - 不要这样做 - 它很奇怪!!

2)您正在每次点击重新创建ScrollView

以下是对我有用的源代码的修改(部分)

class MainApp(App):

    link_buttons = 0

    def build(self):
        layout = GridLayout(cols=1, padding=1, spacing=10,
                    size_hint=(None, None), width=10)
        layout.bind(minimum_height=layout.setter('height'))
        sv = ScrollView(size_hint=(None, None), size=(200, 400),
             pos_hint={'center_x': .5, 'center_y': .5}, do_scroll_x=False)
        sv.add_widget(layout)
        self.layout = layout
        root = RootWidget()
        c = CustomLayout()
        s = Scrollbox()
        root.add_widget(c)
        root.add_widget(s)
        s.add_widget(sv)



        get_buttons = Button(
            text='Get links',
            size_hint=(1, 0),
            pos=(20, 20))
        s.add_widget(get_buttons)

        get_buttons.bind(on_press=self.buttons)
        return root



    def buttons(self, btn):


        layout = self.layout  
        self.link_buttons += 1

        for buttn in range(20):
            btn = Button(text='test', size=(200, 50),
                     size_hint=(None, None))

            self.layout.add_widget(btn)