我对kivy很新,我在调整小部件的大小和定位方面遇到了一些麻烦,我正在尝试生成将在scrollview中的gridlayout中的按钮。当我按下右下角的按钮时,我想生成一些缩放到该按钮宽度的按钮。
但这就是按钮的结果
class RootWidget(BoxLayout):
pass
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):
def build(self):
root = RootWidget()
c = CustomLayout()
root.add_widget(c)
def on_enter(self):
func = Function()
buttons = func.buttons()
root.add_widget(buttons)
get_buttons = Button(
text='Get links',
size_hint=(.5, .10),
pos=(20, 20))
root.add_widget(get_buttons)
get_buttons.bind(on_press=on_enter)
return root
class Function:
def buttons(self):
layout = GridLayout(cols=1, padding=1, spacing=10,
size_hint=(None, None), width=20)
layout.bind(minimum_height=layout.setter('height'))
for buttn in range(20):
btn = Button(text='test', size=(20, 50),
size_hint=(None, None))
layout.add_widget(btn)
# create a scroll view, with a size < size of the grid
root = ScrollView(size_hint=(None, None), size=(20, 500),
pos_hint={'center_x': .5, 'center_y': .5}, do_scroll_x=False)
root.add_widget(layout)
return root
if __name__=='__main__':
MainApp().run()
答案 0 :(得分:1)
我强烈建议您使用kv文件来描述GUI,因为它更容易阅读,因此更容易维护。看看这个例子:
test.kv:
#:kivy 1.9.0
GridLayout:
rows: 1
LeftArea:
RightArea:
<LeftArea@FloatLayout>:
canvas:
Color:
rgb: 0, 1, 0
Rectangle:
size: self.size
pos: self.pos
<RightArea@GridLayout>:
cols: 1
size_hint_x: 0.3
spacing: '10dp'
ScrollView:
LinksGrid:
id: links_grid
GetLinksButton:
links_grid: links_grid
<LinksGrid@GridLayout>:
cols: 1
spacing: '5dp'
size_hint_y: None
height: self.minimum_height
<GetLinksButton>:
size_hint_y: 0.2
text: 'get links'
on_press: self.get_links()
<LinkButton>:
size_hint_y: None
height: '80dp'
main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.button import Button
class GetLinksButton(Button):
link_number = 1
def get_links(self):
for i in xrange(3):
link_button = LinkButton(
text='link number ' + str(self.link_number)
)
self.link_number += 1
self.links_grid.add_widget(link_button)
class LinkButton(Button):
pass
class Test(App):
pass
Test().run()
结果: