我正在爲kivy編寫一個自定義小部件(請參閱this問題),但我發現,由於某些未知原因,有時bind
方法實際上並未綁定回調。綁定在隨機時間工作
在我的課我有這樣的代碼:
def __init__(self, **kwargs):
super(SpinBox, self).__init__(**kwargs)
self._value_label = Label(text=str(self.value))
self.inc_button = TimedButton(text='+')
self.dec_button = TimedButton(text='-')
def inc(inst):
if float(self.value) + float(self.step) <= self.max_value:
self.value += self.step
def dec(inst):
if float(self.value) - float(self.step) >= self.min_value:
self.value -= self.step
self.inc_button.bind(on_press=inc)
self.inc_button.bind(on_time_slice=inc)
self.dec_button.bind(on_press=dec)
self.dec_button.bind(on_time_slice=dec)
# ...
凡TimedButton
是其他自定義類。這是一個Button
子類,它在on_touch_down
啓動一個定時器,如果在一段時間後它沒有收到一個on_touch_up
它認爲按下了長按並且開始每隔幾毫秒使用Clock.schedule_interval
發送on_time_slice
事件。
因此,試圖利用我的自定義類這樣的:
class MyApp(App):
def build(self):
return SpinBox()
MyApp().run()
的value
完全不增加。
如果我做的:
class MyApp(App):
def build(self):
s = SpinBox()
def inc(inst):
s.value += 1
s.inc_button.bind(on_time_slice=inc)
return s
MyApp().run()
價值是每on_time_slice
事件增加。我不明白爲什麼MyApp
類中的綁定工作,而在SpinBox.__init__
方法中的綁定沒有。 我在做什麼錯?
你的TimedButton正是我要找的。你有沒有嘗試過一個功能來加速某個點後的事情?例如,一旦''on_long_press''被調用了20次,時間就會變得更快。 –