2016-02-04 31 views
0

我试图使用kivy进行自定义布局,我需要使用滚动视图按钮生成一个网格布局,并将其放入另一个布局中。我如何更改父类实例的属性

每次我点击生成按钮的按钮时,它会按下“获取链接”按钮,如果我再次单击它,而不是将按钮添加到现有网格布局,则会创建新的网格布局。

这是应用程序之前,我按“得到的链接”按钮 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

对不起,当我做self.link_buttons + = 1它不会更新MainApp中的link_buttons值(我认为)。我如何更改MainApp中link_buttons的值?我试过超级和@property,我仍然在学习,所以我不确定。 –

回答

1

你有几个问题:

1)来自MainApp的函数inherts - 不要那么做 - 它的奇怪!

2)你现在重新创建在每个滚动型点击

这里是一个修改(的一部分),对我

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) 
相关问题