【问题标题】:Animate image on different screen in kivy [closed]kivy中不同屏幕上的动画图像[关闭]
【发布时间】:2018-08-05 18:22:21
【问题描述】:

我正在尝试在切换轮播时自动为放置在另一个类中的图像设置动画。使用按钮它可以正常工作,但不会自动运行。我用“id”尝试了不同的东西,但我对此比较陌生,所以可能存在一个普遍的错误。 通常屏幕管理器中有 2 个屏幕,第二个屏幕通向轮播。由于简单,我让第一个屏幕出来。 我还计划在 Screen1 中播放电影,并希望在用户切换到 Screen2 时停止它。 我认为主要问题是如何控制不同类中的函数。

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.carousel import Carousel
from kivy.uix.label import Label
from kivy.animation import Animation
from kivy.uix.image import Image

Builder.load_string('''

#:import FadeTransition kivy.uix.screenmanager.FadeTransition

<Screen1>:
    name: "screen1"
    Image:
        id: image1
        source: './img/somearrowup.png'
        pos: 205, 145
    Label:
        text: 'Screen down'
    BoxLayout:
        size_hint: .85,.15                  
        Button:
            text: 'Anim1'
            on_release: root.anim1()

<Screen2>:
    name: "screen2"
    Image:
        id: image2
        source: 'somearrowdown.png'
        pos: 205, 55
    Label:
        text: 'Screen Up'
    BoxLayout:
        size_hint: .85,.15
        Button:
            text: 'Anim2'
            on_release: root.anim2()

<Carou>:
    Screen:
        Carousel:
            id: carousel
            on_index: root.on_index(*args)
            direction: 'top'
            Screen1:
            Screen2:

''')

class Screen1(Screen):    
    def anim1(self):
        self.ids.image1.pos = 205, 155
        animation = Animation(pos=(205, 145),t='out_elastic')
        animation.start(self.ids.image1)

class Screen2(Screen):
    def anim2(self):
        self.ids.image2.pos = 205, 55
        animation = Animation(pos=(205, 45),t='out_elastic')
        animation.start(self.ids.image2)

class Carou(BoxLayout):    
    def on_index(self, instance, value):
        if instance.current_slide.name == 'screen2':
            print ("here an animation in Screen2")
            screen = Screen2()  #doesn't work
            screen.anim2()      #doesn't work
        else:
            print ("here an animation in Screen1")
            #...

class StartMenu(App):        
    def build(self):        
        sm = ScreenManager()        
        screen = Screen()        
        screen.add_widget(Carou())
        screen.name = 'carousel'
        sm.add_widget(screen)

        return sm

感谢您的帮助。

【问题讨论】:

    标签: python animation kivy


    【解决方案1】:

    您的问题是在您的on_index() 方法中,您正在创建一个新的Screen2 并调用它的anim2() 方法。您真正想要的是在您的Carousel 中显示的Screen2 实例中调用anim2() 方法。为此,请替换:

    screen = Screen2()
    

    与:

    screen = instance.current_slide
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多