python - Kivy-多个动画和放置
问题描述
谁能帮我放置动画以及如何添加多个动画?所以我得到了我需要的圆圈动画,它工作得很好,但我不仅需要添加另一个更小的动画。但我还需要将它们放置在 PS 草图(https://i.stack.imgur.com/AmKMS.png)上所示的特定位置。所以如果有人可以帮助我,我真的很感激:)
That teh code!
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.animation import Animation
from kivy.properties import NumericProperty
Builder.load_string('''
<Loading>:
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: 0, 0, 1
origin: 1150, 480
canvas.after:
PopMatrix
canvas:
Color:
rgba: .1, 1, .1, .9
Line:
width: 2.
circle:
(self.center_x, self.center_y, min(1150, 480)
/ 2, 90, 180, 10)
<Loading2>:
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: 0, 0, 1
origin: 1150.480
canvas.after:
PopMatrix
canvas:
Color:
rgba: .1, 1, .1, .9
Line:
width: 2.
circle:
(self.center_x, self.center_y, min(1150, 480)
/ 4, 90, 180, 10)`enter code here`
''')
class Loading(FloatLayout):
angle = NumericProperty(0)
def __init__(self, **kwargs):
super(Loading, self).__init__(**kwargs)
anim = Animation(angle = 360, duration=2)
anim += Animation(angle = 360, duration=2)
anim.repeat = True
anim.start(self)
def on_angle(self, item, angle):
if angle == 360:
item.angle = 0
class Loading2(FloatLayout):
angle = NumericProperty(0)
def __init__(self, **kwargs):
super(Loading, self).__init__(**kwargs)
anim = Animation(angle = 360, duration=2)
anim += Animation(angle = 360, duration=2)
anim.repeat = True
anim.start(self)
def on_angle(self, item, angle):
if angle == 360:
item.angle = 0
class TestApp(App):
def build(self):
return Loading()
return Loading2()
TestApp().run()
解决方案
您的代码存在一些问题:
- 该
build()
方法(或任何方法)只能执行一个return
,因此您的第二个return
将被忽略。 - 你的论据太多了
circle
。 origin
你的Rotate
可能不在屏幕上。我想你想围绕圆心旋转。super()
inLoading2
引用而Loading
不是Loading2
.- 有几个缩进错误(可能只是来自代码的复制/粘贴)。
因此,这是您的代码的修改版本,可以满足您的大部分需求:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.animation import Animation
from kivy.properties import NumericProperty
kv = '''
FloatLayout:
Loading:
size_hint: 0.3, 1
Loading2:
size_hint: 0.7, 1
pos_hint: {'right':1}
<Loading>:
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
canvas:
Color:
rgba: .1, 1, .1, .9
Line:
width: 2.
circle:
(root.center_x, root.center_y, 100, 90, 180, 10)
<Loading2>:
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
canvas:
Color:
rgba: .1, 1, .1, .9
Line:
width: 2.
circle:
(root.center_x, root.center_y, 200, 90, 180, 10)
'''
class Loading(FloatLayout):
angle = NumericProperty(0)
def __init__(self, **kwargs):
super(Loading, self).__init__(**kwargs)
anim = Animation(angle = 360, duration=2)
anim += Animation(angle = 360, duration=2)
anim.repeat = True
anim.start(self)
def on_angle(self, item, angle):
if angle == 360:
item.angle = 0
class Loading2(FloatLayout):
angle = NumericProperty(0)
def __init__(self, **kwargs):
super(Loading2, self).__init__(**kwargs)
anim = Animation(angle = 360, duration=2)
anim += Animation(angle = 360, duration=2)
anim.repeat = True
anim.start(self)
def on_angle(self, item, angle):
if angle == 360:
item.angle = 0
class TestApp(App):
def build(self):
return Builder.load_string(kv)
TestApp().run()
我已经FloatLayout
为您的. 添加了一个根目录kv
,因此Loading
和Loading2
类都在您的 GUI 中。
推荐阅读
- django - 我无法在我的帖子中添加类别网址
- python - 错误捕获结合计数器不起作用
- python - 如果使用Python满足条件,则查找列表元素的平均值?
- user-interface - 如何在 Flutter 中设计兼容屏幕分辨率的 UI?
- javascript - .env 调用不同文件中的变量
- python-3.x - Pandas isin vs Numpy in1d
- laravel - Laravel 与 vagrant/homestead 的新项目
- android - Qt Android:有没有办法用初始化列表初始化 std::map?
- html - 带有特殊字符的 Markdown TOC?
- matlab - Matlab:为什么我不能使用 VideoWriter 创建一个 mp4 文件?