正如标题所说,我试图在SparseGridLayout上移动小部件。原因是在高辐射环境中测试显示器。在以前的代码迭代中,我在没有SparseGridLayout的情况下设置了动画小部件,但它们无法调整大小。当我搬到SparsGridLayout时,他们消失了!经过一番工作,我创建了第二个FloatLayout。现在我看到了小部件,但无论我做什么,它们都是静止的!我一直在使用Kivy Crash Course的代码来获取动画,并修改了inclem.net的布局代码。我使用的是kivy 1.9.1和python 2.7。以下是一些代码:
`<CombinedLayout>:
SparseGridLayout:
BlackHorizontalRect
BlackVerticalRect
.
.
.
class CombinedLayout(FloatLayout):
pass
.
.
.
class BlackHorizontalRect(Widget):
velocity = ListProperty([0, 2])
def __init__(self, **kwargs):
super(BlackHorizontalRect,self).__init__(**kwargs)
Clock.schedule_interval(self.update, 1/60.)
def update(self, *args):
self.x += self.velocity[0]
self.y += self.velocity[1]
if self.x <0 or (self.x + self.width) > Window.width:
self.velocity[0] *= -1
if self.y <0 or (self.y + self.height) > Window.height:
self.velocity[1] *= -1
class SparseGridLayout(FloatLayout):
rows = NumericProperty(1)
columns = NumericProperty(1)
shape = ReferenceListProperty(rows, columns)
def do_layout(self, *args):
shape_hint = (1. / self.columns, 1. / self.rows)
for child in self.children:
child.size_hint = shape_hint
if not hasattr(child, 'row'):
child.row = 0
if not hasattr(child, 'column'):
child.column = 0
child.pos_hint = {'x': shape_hint[0] * child.row,
'y': shape_hint[1] * child.column}
super(SparseGridLayout, self).do_layout(*args)
class ExampleApp(App):
def build(self):
labels = []
for i in range(5):
for j in range(5):
if (i == 0 and j==2) or (i ==1 and j ==1) or (i == 2 and j ==0):
labels.append(GridLabel0(row=i, column=j))
if (i == 1 and j == 2) or (i == 2 and j == 1) or (i == 3 and j == 0):
labels.append(GridLabel1(row=i, column=j))
if (i == 2 and j==2) or (i==3 and j==1) or (i ==4 and j==0):
labels.append(GridLabel2(row=i, column=j))
if (i == 3 and j ==2) or (i==4 and j==1) or (i==0 and j==0):
labels.append(GridLabel3(row=i, column=j))
if (i == 4 and j==2) or (i==0 and j==1) or (i==1 and j==0):
labels.append(GridLabel4(row=i, column=j))
layout = SparseGridLayout(rows=3, columns=5)
for label in labels:
layout.add_widget(label)
c = CombinedLayout()
wid1 = BlackVerticalRect()
wid2 = BlackHorizontalRect()
c.add_widget(layout, index = 1)
c.add_widget(wid1)
c.add_widget(wid2)
return c
if __name__ == '__main__':
ExampleApp().run()'
非常感谢任何帮助。