파이썬[Python]: kivy - bind 함수 with Button_0004

kivy.event 모듈 - EventDispatcher 클래스 - bind 함수(functions)


/// 설명

이벤트 타입이나 속성을 callback 함수에 연결합니다.

Event: on_press, on_release, state

사용: ProgressBar(**kwargs)

참고: event 모듈 목록

※ 형식
bind(**kwargs)

reference
https://kivy.org/doc/stable/api-kivy.event.html

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import kivy
 
kivy.require('2.0.0')
 
import os
from kivy.app import App
from kivy.core.window import Window
from kivy.core.audio import SoundLoader
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.progressbar import ProgressBar
 
os.environ['KIVY_AUDIO'= 'ffpyplayer'  # install ffpyplayer
Window.clearcolor = (.61, .81)
Window.size = (5631001)
Window.top, Window.left = 30800
 
 
class FantasticApp(App):
    _init = True
    _sound = False
    _total_sec = 0.0
    _min, _sec = 00
 
    def _time_format(self):
        self._min = self._total_sec // 60
        self._sec = self._total_sec % 60
        self.bt4_root.text = f'Total: {int(self._total_sec)} second(s)' + \
                             f' - {int(self._min)}:{int(self._sec)}'
 
    def _bt_1s(self, bt_instance):
        if self._init == True:
            self._total_sec += 1
            self._time_format()
 
    def _bt_5s(self, bt_instance):
        if self._init == True:
            self._total_sec += 5
            self._time_format()
 
    def _bt_10s(self, bt_instance):
        if self._init == True:
            self._total_sec += 10
            self._time_format()
 
    def _bt_start(self, bt_instance):
        if bt_instance.text == 'Start':
            bt_instance.text = 'Stop'
            if FantasticApp._init == True:
                self.pgb.max = self._total_sec
                self.pgb.value = self._total_sec
                self._init = False
 
            # call _clock_call() every 0.1 seconds
            self.event = Clock.schedule_interval(self._clock_call, 0.1)
        else:
            bt_instance.text = 'Start'
            self.event.cancel()
            if self._sound:
                self._sound.stop()
 
    def _bt_reset(self, bt_instance):
        self.bt2_root.text = 'Start'
        self.bt4_root.text = 'Total: second(s)'
        self.pgb.max = 1
        self.pgb.value = 1
        self._init = True
        self._total_sec = 0.0
        self._min, self._sec = 00
        if self._sound:
            self._sound.stop()
 
    def _clock_call(self, clock_instance):
        if self.pgb.value > 0.0:
            self._total_sec -= 0.1
            self.pgb.value = self._total_sec
            self.bt4_root.text = f'CountDown: {int(self._total_sec + 1)} second(s)'
        else:
            self.bt4_root.text = f'CountDown: 0 second(s)'
            self.event.cancel()
            self._sound = SoundLoader.load('thunder.wav')
            if self._sound:
                self._sound.loop = True
                self._sound.play()
 
    def build(self):
        # You can make this 'App' using 'Class'
        bl_top = BoxLayout()
        # add 1 second
        bt1 = Button(text='+ 1 second', on_press=self._bt_1s)
        # add 5 seconds
        bt2 = Button(text='+ 5 seconds', on_press=self._bt_5s)
        # add 10 seconds
        bt3 = Button(text='+ 10 seconds', on_press=self._bt_10s)
        bl_top.add_widget(bt1)
        bl_top.add_widget(bt2)
        bl_top.add_widget(bt3)
 
        bt1_root = Button(text='Timer: Set the second(s)')
        # reset
        bt3_root = Button(text='Reset', on_press=self._bt_reset)
        # start and stop
        self.bt2_root = bt2_root = Button(text='Start', on_press=self._bt_start)
        # totoal second
        self.bt4_root = bt4_root = Button(text='Total: second(s)', font_size=30)
        self.pgb = pgb = ProgressBar(max=1, value=1)
 
        # root BoxLayout
        bl_root = BoxLayout(orientation='vertical')
        bl_root.add_widget(bl_top)
        bl_root.add_widget(bt1_root)
        bl_root.add_widget(bt2_root)
        bl_root.add_widget(bt3_root)
        bl_root.add_widget(pgb)
        bl_root.add_widget(bt4_root)
 
        return bl_root
 
 
FantasticApp().run()
 
cs

/// 출력

/// 사운드 파일


* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

C 언어: sin 함수, cos 함수, tan 함수

파이썬[Python]: 내장함수 - bit_length 메서드