Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have created a timer that counts down from a random number to zero, then once it has hit zero it moves to a new screen and counts down from a random number again. I would like for there to be a sound as the timer reaches 3, 2, 1 and then a different sound of 0, but I am unsure of how to implement this.

I have tried loading the sound file as an mp3 through SoundLoader and the function is called when the button is pressed to begin the timer. My current understanding is that the function will check if self.a =3,2,1 or 0 only once as the function is called, so the kivy.clock Clock.schedule_interval() function is required to check the value of self.a every second. I have tried this (shown in the code below), but the timer runs for one second and then crashes, displaying the error message TypeError: sound() takes 1 positional argument but 2 were given

py file

from kivy.app import App

from kivy.lang import Builder

from kivy.uix.screenmanager import ScreenManager, Screen

from kivy.animation import Animation

from kivy.properties import NumericProperty

from kivy.uix.togglebutton import ToggleButton

from random import randint

from kivy.uix.popup import Popup

from kivy.uix.label import Label

from kivy.core.audio import SoundLoader

from kivy.clock import Clock

class MainWindow(Screen):

    pass

class SecondWindow(Screen):

    pass

class ThirdWindow(Screen):

    pass

class FourthWindow(Screen):

    pass

class WindowManager(ScreenManager):

    a = NumericProperty(0)

    b = NumericProperty(0)

    run_t = NumericProperty(30)

    min = NumericProperty(5)

    max = NumericProperty(5)

    sound_done = SoundLoader.load('beep-01a.mp3')

    sound_finishing = SoundLoader.load('beep-07.mp3')

    def sound(self):

        Clock.schedule_interval(self.sound, 1)

        if self.a == 0:

            self.sound_done.play()

        if self.a == 1:

            self.sound_finishing.play()

        if self.a == 2:

            self.sound_finishing.play()

        if self.a == 3:

            self.sound_finishing.play()

    def proceed(self):

            self.reset()

            self.start()

            self.count_up()

            self.sound()

            self.ids.main_window.manager.current = 'low'

    def reset(self):

        self.a = 0

        self.b = 0

    def start(self, *args):

        self.a = randint(self.min, self.max)

        self.anim = Animation(a = 0, duration = self.a)

            if rand == 0:

                self.ids.main_window.manager.current = 'low'

            elif rand == 1:

                self.ids.main_window.manager.current = 'medium'

            elif rand == 2:

                self.ids.main_window.manager.current = 'high'

            self.anim.bind(on_complete = self.start)

        self.anim.start(self)

    def count_up(self):

        self.anim = Animation(b = self.run_t, duration = self.run_t)

        self.anim.bind(on_complete = self.finish_callback)

        self.anim.start(self)

    def finish_callback(self, animation, param):

        Animation.cancel_all(self)

kv = Builder.load_file("final.kv")

class PageScrollerApp(App):

    def build(self):

        return kv

if __name__ == "__main__":

    PageScrollerApp().run()

kv file

WindowManager:

    MainWindow:

        id: main_window

    SecondWindow:

        id: second_window

    ThirdWindow:

        id: third_window

    FourthWindow:

        id: fourth_window

<MainWindow>:

    name: "home"

    FloatLayout:

        Button:

            pos_hint: {"x":0.4, "y":0.05}

            size_hint: 0.2, 0.1

            text: "Go!"

            on_release:

                root.manager.proceed()

<SecondWindow>:

    name: 'low'

    FloatLayout:

        Label:

            id: count_down1

            text: str(round(root.manager.a, 1))

            size_hint: 0.28, 0.1

            pos_hint: {"x": 0.36, "y": 0.55}

        Label:

            id: count_up1

            text: str(round(root.manager.b, 1))

            size_hint: 0.28, 0.1

            pos_hint: {"x": 0.36, "y": 0.3}

<ThirdWindow>:

    name: "medium"

    FloatLayout:

        Label:

            id: count_down2

            text: str(round(root.manager.a, 1))

            size_hint: 0.28, 0.1

            pos_hint: {"x": 0.36, "y": 0.55}

        Label:

            id: count_up2

            text: str(round(root.manager.b, 1))

            size_hint: 0.28, 0.1

            pos_hint: {"x": 0.36, "y": 0.3}

<FourthWindow>:

    name: "high"

    FloatLayout:

       Label:

            id: count_down3

            text: str(round(root.manager.a, 1))

            size_hint: 0.28, 0.1

            pos_hint: {"x": 0.36, "y": 0.55}

        Label:

            id: count_up3

            text: str(round(root.manager.b, 1))

            size_hint: 0.28, 0.1

            pos_hint: {"x": 0.36, "y": 0.3}

1 Answer

0 votes
by (25.1k points)

You need to use a lambda function with Clock.schedule_interval. Replace Clock.schedule_interval(self.sound, 1) with Clock.schedule_interval(lambda dt: self.sound(), 1)

Related questions

0 votes
1 answer
asked Oct 4, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 12, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Dec 6, 2020 in AWS by devin (5.6k points)

Browse Categories

...