Как сделать мигающий вариант текста - PullRequest
0 голосов
/ 25 октября 2019
Builder.load_string("""
<ScreenOne>:
    FloatLayout:
        orientation: 'horizontal'
        canvas:
            Rectangle:
                source: 'back1.jpg'
                size: self.size
                pos: self.pos
    BoxLayout:
        Button:
            background_color: 0, 0, 0, 0
            on_press:
                # You can define the duration of the change
                # and the direction of the slide
                root.manager.transition.direction = 'up'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_two'
                App.blink_animation(blinky)   
    BoxLayout: 

# So over here I tried to create a variable 'alpha' for the opacity,
# that I try to change using animation below.

        Label:
            id: blinky
            text: "Click Anywhere To Continue"
            font_size: '20sp'
            font_name: "Raleway-Regular"
            size_hint: (1.0, 0)
            alpha: 1 
            color: (1, 1, 1, self.alpha)


<ScreenTwo>:
    FloatLayout:
        orientation: 'horizontal'
        canvas:
            Rectangle:
                source: 'back2.jpg'
                size: self.size
                pos: self.pos
    BoxLayout:
        Button:
            background_color: 0, 0, 0, 0
            on_press:
                root.manager.transition.direction = 'down'
                root.manager.current = 'screen_one'
""")

# Create a class for all screens in which you can include
# helpful methods specific to that screen

Config.set('graphics', 'resizable', '0') #0 being off 1 being on as in true/false
Config.set('graphics', 'width', '960')
Config.set('graphics', 'height', '720')

class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):
    pass


# The ScreenManager controls moving between screens
screen_manager = ScreenManager()

# Add the screens to the manager and then supply a name
# that is used to switch screens
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))

class KivyTut2App(App):
    def blink_animation(self, blinky, *args):
        anim = Animation(alpha=0, duration=0.5) + Animation(alpha=0, duration=0.5)
        anim += Animation(alpha=1, duration=0.5) + Animation(alpha=1, duration=0.5)
        anim.repeat = True
        anim.start(blinky)
    def build(self):
        return screen_manager

sample_app = KivyTut2App()
sample_app.run()

В первой выбранной выше цитате блока я пытаюсь создать параметр self.alpha, а во второй выбранной выше цитате блока я пытаюсь использовать self.alpha для изменения непрозрачности и создания анимации. Вывод - это просто текст без анимации, и я хотел бы узнать, где я ошибся.

1 Ответ

0 голосов
/ 25 октября 2019

Я считаю, что ваш код в основном должен работать, однако есть некоторые проблемы. Если blinky Label является частью ScreenOne, то вы не увидите мигания, потому что нажатие Button, которое начинает мигание, также переключается на ScreenTwo. Нажатие Button на ScreenTwo возвращает к ScreenOne, и мигание должно быть видно.

Если blinky является частью ScreenTwo, то blinky id недопустимо в пределахScreenOne.

Если вы хотите, чтобы мигание начиналось без нажатия Button, затем вызовите метод blink_animation() из метода build(), используя Clock.schedule_once(). Сначала удалите вызов blink_animation() из Button on_press:, например:

    Button:
        background_color: 0, 0, 0, 0
        on_press:
            # You can define the duration of the change
            # and the direction of the slide
            root.manager.transition.direction = 'up'
            root.manager.transition.duration = 1
            root.manager.current = 'screen_two'

Затем вызовите метод blink_animation() из метода build() и измените blink_animation()Метод:

class KivyTut2App(App):
    def blink_animation(self, dt):
        anim = Animation(alpha=0, duration=0.5) + Animation(alpha=1, duration=0.5)
        anim.repeat = True
        anim.start(screen_manager.get_screen('screen_one').ids.blinky)
    def build(self):
        Clock.schedule_once(self.blink_animation)
        return screen_manager
...