ユーザーがクリック/タッチしたときに注目したいテキスト入力があります。(かなり標準!) DragableObject (kivy wiki のユーザー例) と GridLayout から継承します。
class DragableObject( Widget ):
def on_touch_down( self, touch ):
if self.collide_point( *touch.pos ):
touch.grab( self )
return True
def on_touch_up( self, touch ):
if touch.grab_current is self:
touch.ungrab( self )
return True
def on_touch_move( self, touch ):
if touch.grab_current is self:
self.pos = touch.x-self.width/2, touch.y-self.height/2
class MyWidget(DragableObject, GridLayout):
def __init__(self, **kwargs):
kwargs['orientation'] = 'lr-tb'
kwargs['spacing'] = 10
kwargs['size_hint'] = (None, None)
kwargs['size'] = (200, 80)
self.cols = 2
super(MyWidget, self).__init__(**kwargs)
with self.canvas:
self.rect = Rectangle(pos=self.pos, size=self.size)
with self.canvas.before:
Color(0, .5, 1, mode='rgb')
self.bind(pos=self.update_rect)
self.bind(size=self.update_rect)
self.add_widget(Label(text='t1'))
self.text1 = TextInput(multiline=False)
self.add_widget(self.text1)
self.add_widget(Label(text='t2'))
self.text2 = TextInput(multiline=False)
self.add_widget(self.text2)
# these bind's don't seem to work
self.text1.bind(on_touch_down=self.set_focus)
self.text1.bind(on_touch_up=self.set_focus)
self.text1.bind(on_touch_move=self.set_focus)
def set_focus(self):
print("pressed")
self.text1.focus = True
def update_rect(self, *args):
self.rect.pos = self.pos
self.rect.size = self.size
2 つの問題があります。
を。テキスト入力はフォーカスできません。
b. textinput ウィジェットで動作するイベント コールバック (on_touch_down など) を取得できません。
何か案は?