kivyで作ったスロットマシンのコードです。15,16行目の
python
1def change_value(self,dt): 2 self.value = random.randint(0,9) 3
の引数のdtはなぜ必要なのでしょうか。試しにdtを消して実行すると
`Traceback (most recent call last):
File "main.py", line 53, in <module>
SlotApp().run()
File "/Users/babayoshimichi/.pyenv/versions/3.6.5/lib/python3.6/site-packages/kivy/app.py", line 855, in run
runTouchApp()
File "/Users/babayoshimichi/.pyenv/versions/3.6.5/lib/python3.6/site-packages/kivy/base.py", line 504, in runTouc
hApp
EventLoop.window.mainloop()
File "/Users/babayoshimichi/.pyenv/versions/3.6.5/lib/python3.6/site-packages/kivy/core/window/window_sdl2.py", l
ine 747, in mainloop
self._mainloop()
File "/Users/babayoshimichi/.pyenv/versions/3.6.5/lib/python3.6/site-packages/kivy/core/window/window_sdl2.py", l
ine 479, in _mainloop
EventLoop.idle()
File "/Users/babayoshimichi/.pyenv/versions/3.6.5/lib/python3.6/site-packages/kivy/base.py", line 339, in idle
Clock.tick()
File "/Users/babayoshimichi/.pyenv/versions/3.6.5/lib/python3.6/site-packages/kivy/clock.py", line 591, in tick
self._process_events()
File "kivy/_clock.pyx", line 384, in kivy._clock.CyClockBase._process_events
File "kivy/_clock.pyx", line 414, in kivy._clock.CyClockBase._process_events
File "kivy/_clock.pyx", line 412, in kivy._clock.CyClockBase._process_events
File "kivy/_clock.pyx", line 167, in kivy._clock.ClockEvent.tick
TypeError: change_value() takes 1 positional argument but 2 were given`
というエラーが出ます。ご教示お願いいたします。
python
1from kivy.app import App 2from kivy.clock import Clock 3from kivy.properties import NumericProperty 4from kivy.uix.boxlayout import BoxLayout 5from kivy.uix.button import Button 6from kivy.uix.label import Label 7import random 8 9class SlotLabel(Label): 10 value = NumericProperty(0) 11 font_size = 48 12 evt = None 13 def on_value(self,*args): 14 self.text = str(self.value) 15 def change_value(self): 16 self.value = random.randint(0,9) 17 18class SlotButton(Button): 19 counter = 0 20 font_size = 28 21 def on_press(self): 22 upper_box = self.parent.children[1] 23 if self.counter == 0: 24 for lbl in upper_box.children: 25 lbl.evt = Clock.schedule_interval(lbl.change_value, 0.05) 26 self.text = 'Stop' 27 elif self.counter == 4: 28 for lbl in upper_box.children: 29 lbl.value = 0 30 self.text = 'Start' 31 else: 32 lbl = upper_box.children[3-self.counter] 33 lbl.evt.cancel() 34 if self.counter == 3: 35 self.text = 'Reset' 36 self.counter += 1 37 if self.counter == 5: 38 self.counter = 0 39 40class SlotApp(App): 41 def build(self): 42 root_box = BoxLayout(orientation='vertical') 43 upper_box = BoxLayout(size_hint_y=9) 44 45 upper_box.add_widget(SlotLabel(text='0')) 46 upper_box.add_widget(SlotLabel(text='0')) 47 upper_box.add_widget(SlotLabel(text='0')) 48 49 root_box.add_widget(upper_box) 50 root_box.add_widget(SlotButton(text='Start')) 51 return root_box 52 53SlotApp().run()
回答1件
あなたの回答
tips
プレビュー