检测kivy textinput中的点击/触摸

时间:2013-10-08 01:09:35

标签: python user-interface kivy

我有一个textinput,我想在用户点击/触摸它时关注它。 (相当标准!)它继承自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

我有两个问题。

一个。文本输入无法聚焦。

湾我无法获得事件回调(例如on_touch_down)来处理textinput小部件。

有什么想法吗?

2 个答案:

答案 0 :(得分:9)

简答

您可以简单地使用Scatter。它包括拖动,旋转和缩放功能,您可以停用其中任何一个或全部:

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

您描述的所有问题都不应发生在Scatter

长答案

您的问题是您要覆盖父母的on_touch_downon_touch_moveon_touch_up

通常这些方法会调用相应的孩子。例如,当调用on_touch_down实例的Widget方法时,Widget实例将遍历其子项,然后调用每个on_touch_down方法(如果您熟悉递归和树结构,我们讨论的是递归遍历方法 - 我认为是预订 - Tree traversal)。

在DraggableObject类中重写此功能。 您必须确保使用以下方法调用基类方法:

super(DraggableWidget,self).on_touch_down(touch)

根据您要查找的行为,这些方法可能如下所示:

(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

在这种情况下,当其中一个子项处理事件时,您将避免拖动Widget。这一切都取决于你做了什么。

答案 1 :(得分:0)

很抱歉在这里提问,但我认为你们可以调试这个问题。 我无法解决这个问题

KIVY: Carousel inside another carousel

相关问题