파이썬[Python]: kivy - bind 함수 with Button_0003
kivy.event 모듈 - EventDispatcher 클래스 - bind 함수(functions)
/// 설명
이벤트 타입이나 속성을 callback 함수에 연결합니다.
Event: on_press, on_release, state
사용: Clock.schedule_interval()
참고: event 모듈 목록
※ 형식
bind(**kwargs)
reference
https://kivy.org/doc/stable/api-kivy.event.html
Event: on_press, on_release, state
사용: Clock.schedule_interval()
참고: 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 | import kivy kivy.require('2.0.0') from kivy.app import App from kivy.core.window import Window from kivy.clock import Clock from kivy.uix.boxlayout import BoxLayout from kivy.uix.button import Button Window.clearcolor = (.6, 1, .8, 1) Window.size = (563, 1001) Window.top, Window.left = 30, 800 class FantasticApp(App): _init_mic, _init_sec, _init_min = 0, 0, 0 _lab_items = 1 def _bt_start(self, bt_instance): if bt_instance.text == 'Start': # call _clock_call() every 0.01 seconds self.event = Clock.schedule_interval(self._clock_call, 0.01) bt_instance.text = 'Stop' else: self.event.cancel() bt_instance.text = 'Start' def _bt_lab(self, bt_instance): if self._lab_items < 11: lab_items = '{}: {:0>2}:{:0>2}.{:0>2}'.format(self._lab_items, str(self._init_min), str(self._init_sec), str(self._init_mic)) bt_lab = Button(text=lab_items) self.bl_root.add_widget(bt_lab) self._lab_items += 1 else: self.bt3.text = 'Max: You can not add the widget' def _bt_reset(self, bt_instance): for i in self.bl_root.children[0:(self._lab_items - 1)]: self.bl_root.remove_widget(i) self.bt2.text = '00:00.00' self.bt3.text = 'Lab List' self._init_mic, self._init_sec, self._init_min = 0, 0, 0 self._lab_items = 1 def _clock_call(self, clock_instance): self.bt2.text = '{:0>2}:{:0>2}.{:0>2}'.format(str(self._init_min), str(self._init_sec), str(self._init_mic)) self._init_mic += 1 if self._init_mic > 99: self._init_mic = 0 self._init_sec += 1 if self._init_sec > 59: self._init_sec = 0 self._init_min += 1 if self._init_min > 59: # max: 1 hour self._init_mic = 0 self._init_sec = 0 self._init_min = 0 def build(self): # You can make this APP using CLASS # start and stop bt_top1 = Button(text='Start') bt_top1.bind(on_press=self._bt_start) # Lab bt_top2 = Button(text='Lap') bt_top2.bind(on_press=self._bt_lab) bl_top = BoxLayout(size_hint=(1, None), height=150) bl_top.add_widget(bt_top1) bl_top.add_widget(bt_top2) # reset bt1 = Button(text='Reset', size_hint=(1, None), height=150, on_press=self._bt_reset) # time self.bt2 = bt2 = Button(text='00:00.00', size_hint=(1, None), height=150, font_size=50) # Lab List self.bt3 = bt3 = Button(text='Lab List') # root BoxLayout self.bl_root = bl_root = BoxLayout(orientation='vertical') bl_root.add_widget(bl_top) bl_root.add_widget(bt1) bl_root.add_widget(bt2) bl_root.add_widget(bt3) return bl_root FantasticApp().run() | cs |
/// 출력
* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)
– 당신을 응원합니다. –
댓글
댓글 쓰기