2

ユーザーがクリック/タッチしたときに注目したいテキスト入力があります。(かなり標準!) 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 など) を取得できません。

何か案は?

4

2 に答える 2

10

簡潔な答え

を簡単に使用できますScatter。ドラッグ、回転、およびスケーリング機能が含まれており、それらの一部またはすべてを無効にすることができます。

my_scatter = Scatter(do_rotation=False, do_scale=False) 

あなたが説明した問題はどれも、Scatter

長い答え

あなたの問題は、親の と をon_touch_downオーバーライドon_touch_moveしていることです。on_touch_up

通常、これらのメソッドは対応する子のメソッドを呼び出します。たとえば、インスタンスのon_touch_downメソッドWidgetが呼び出されると、Widgetインスタンスはその子をトラバースし、on_touch_downそれぞれのメソッドを呼び出します (再帰とツリー構造に精通している場合は、再帰的トラバース メソッドについて話します。事前注文を考えてください-ツリートラバーサル)。

この機能は、DraggableObject クラスでオーバーライドされます。基本クラスのメソッドを次のように呼び出す必要があります。

super(DraggableWidget, self).on_touch_down(タッチ)

探している動作に応じて、メソッドは次のようになります。

(1) 常に子を呼び出したい場合:

def on_touch_down( self, touch ):
    if self.collide_point( *touch.pos ):
        touch.grab( self )
    return super(DraggableWidget, self).on_touch_down(touch)

(2)衝突がないときに子を呼び出したいだけの場合:

def on_touch_down( self, touch ):
    if self.collide_point( *touch.pos ):
        touch.grab( self )
        return True      # Don't call the on_touch_down of the Base Class
    return super(DraggableWidget, self).on_touch_down(touch)

そして、より多くのオプションがあります!. 戻り値は、イベントが子によって処理されたかどうかを示します。たとえば、次のようなことができます。

def on_touch_down( self, touch ):
    handled = super(DraggableWidget, self).on_touch_down(touch)
    if not handled and self.collide_point( *touch.pos ):
        touch.grab( self )
        return True
    return handled

この場合、子の 1 つがイベントを処理するときにウィジェットがドラッグされるのを回避できます。それはすべてあなたが何をするかにかかっています。

于 2013-10-08T08:12:54.593 に答える