0
我试图使用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的值?我试过超级和@property,我仍然在学习,所以我不确定。 –