如何使用 Kivy 在加载动画上放置一个按钮?
How to place a button on a loading animation with Kivy?
我是 Kivy 的新手,我正在尝试制作我的第一个应用程序,但我并不真正了解如何使用元素和 类...
我想放一个按钮来停止声音,但它只会停止动画....
这是代码,我想我没有正确编码! :(
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
from kivy.core.audio import SoundLoader
from kivy.uix.button import Button
from functools import partial
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<App_container>:
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
Image:
id: img_anim
source: 'logo.png'
size_hint: 0,0
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
''')
class App_container(FloatLayout):
angle = NumericProperty(0)
def __init__(self, **kwargs):
#Anim
super(App_container, self).__init__(**kwargs)
anim = Animation(angle = 360, duration=2)
anim2 = Animation(size_hint=(2,2), duration=2)
anim.start(self)
anim2.start(self.ids["img_anim"])
#Son
self.sound = SoundLoader.load('zik.wav')
self.sound.loop = True
self.sound.play()
#boutonzik
btn = Button(text ="Push Me !")
self.add_widget(btn)
btn.bind(on_press=partial(self.foo, btn))
def foo(self, instance, *args):
self.sound.volume=0
class TestApp(App):
def build(self):
return App_container()
if __name__ == "__main__":
app = TestApp()
app.run()
为了Button
不旋转,它一定不能在旋转Layout
中。为此,您可以在 App_container
中添加另一个 FloatLayout
,并且只旋转那个 FloatLayout
。对您的代码进行以下修改:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.animation import Animation
from kivy.core.audio import SoundLoader
Builder.load_string('''
<App_container>:
FloatLayout:
# move the angle property into this FloatLayout
angle: 0.0
id: rotate_this
canvas.before:
PushMatrix
Rotate:
angle: self.angle
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
Image:
id: img_anim
source: 'logo.png'
size_hint: 0,0
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
text: 'Push Me'
on_press: root.foo(self)
size_hint: 0.1,0.1
pos_hint: {'center_x':0.5, 'center_y':0.5}
''')
class App_container(FloatLayout):
def __init__(self, **kwargs):
#Anim
super(App_container, self).__init__(**kwargs)
anim = Animation(angle = 360, duration=2)
anim2 = Animation(size_hint=(2,2), duration=2)
# rotate the FloatLayout with id "rotate_this"
anim.start(self.ids["rotate_this"])
# animate the "img_anim"
anim2.start(self.ids["img_anim"])
#Son
self.sound = SoundLoader.load('zik.wav')
self.sound.loop = True
self.sound.play()
def foo(self, instance, *args):
self.sound.volume=0
class TestApp(App):
def build(self):
return App_container()
if __name__ == "__main__":
app = TestApp()
app.run()
所以 FloatLayout
自旋,但 Button
不自旋,因为它不在自旋 FloatLayout
内。
我是 Kivy 的新手,我正在尝试制作我的第一个应用程序,但我并不真正了解如何使用元素和 类...
我想放一个按钮来停止声音,但它只会停止动画....
这是代码,我想我没有正确编码! :(
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
from kivy.core.audio import SoundLoader
from kivy.uix.button import Button
from functools import partial
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<App_container>:
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
Image:
id: img_anim
source: 'logo.png'
size_hint: 0,0
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
''')
class App_container(FloatLayout):
angle = NumericProperty(0)
def __init__(self, **kwargs):
#Anim
super(App_container, self).__init__(**kwargs)
anim = Animation(angle = 360, duration=2)
anim2 = Animation(size_hint=(2,2), duration=2)
anim.start(self)
anim2.start(self.ids["img_anim"])
#Son
self.sound = SoundLoader.load('zik.wav')
self.sound.loop = True
self.sound.play()
#boutonzik
btn = Button(text ="Push Me !")
self.add_widget(btn)
btn.bind(on_press=partial(self.foo, btn))
def foo(self, instance, *args):
self.sound.volume=0
class TestApp(App):
def build(self):
return App_container()
if __name__ == "__main__":
app = TestApp()
app.run()
为了Button
不旋转,它一定不能在旋转Layout
中。为此,您可以在 App_container
中添加另一个 FloatLayout
,并且只旋转那个 FloatLayout
。对您的代码进行以下修改:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.animation import Animation
from kivy.core.audio import SoundLoader
Builder.load_string('''
<App_container>:
FloatLayout:
# move the angle property into this FloatLayout
angle: 0.0
id: rotate_this
canvas.before:
PushMatrix
Rotate:
angle: self.angle
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
Image:
id: img_anim
source: 'logo.png'
size_hint: 0,0
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Button:
text: 'Push Me'
on_press: root.foo(self)
size_hint: 0.1,0.1
pos_hint: {'center_x':0.5, 'center_y':0.5}
''')
class App_container(FloatLayout):
def __init__(self, **kwargs):
#Anim
super(App_container, self).__init__(**kwargs)
anim = Animation(angle = 360, duration=2)
anim2 = Animation(size_hint=(2,2), duration=2)
# rotate the FloatLayout with id "rotate_this"
anim.start(self.ids["rotate_this"])
# animate the "img_anim"
anim2.start(self.ids["img_anim"])
#Son
self.sound = SoundLoader.load('zik.wav')
self.sound.loop = True
self.sound.play()
def foo(self, instance, *args):
self.sound.volume=0
class TestApp(App):
def build(self):
return App_container()
if __name__ == "__main__":
app = TestApp()
app.run()
所以 FloatLayout
自旋,但 Button
不自旋,因为它不在自旋 FloatLayout
内。