【问题标题】:How can I change an attribute of a parents class instance如何更改父类实例的属性
【发布时间】:2016-02-05 05:21:08
【问题描述】:

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

每次我单击生成按钮的按钮时,它都会向上推动“获取链接”按钮,如果我再次单击它而不是向现有的网格布局添加按钮,它会创建一个新的网格布局。

这是我按下“获取链接”按钮之前的应用程序

我第一次按下“获取链接”按钮时

第二次按下按钮

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

【问题讨论】:

  • 你现在的问题是什么?
  • 是的,抱歉,当我执行 self.link_buttons += 1 时,它不会更新 MainApp 中的 link_buttons 值(我认为)。如何更改 MainApp 中 link_buttons 的值?我已经尝试过 super 和 @property,我还在学习,所以我不确定。

标签: python kivy


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2021-11-07
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    相关资源
    最近更新 更多