【问题标题】:detecting click/touch in kivy textinput在 kivy 文本输入中检测点击/触摸
【发布时间】:2013-10-08 01:09:34
【问题描述】:

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

我有两个问题。

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

b.我无法让事件回调(例如 on_touch_down)在 textinput 小部件上工作。

有什么想法吗?

【问题讨论】:

    标签: python user-interface kivy


    【解决方案1】:

    简答

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

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

    你描述的问题都不应该发生在Scatter

    长答案

    您的问题是您正在覆盖父级的on_touch_downon_touch_moveon_touch_up

    通常这些方法会调用相应的子方法。例如,当调用Widget 实例的on_touch_down 方法时,则Widget 实例将遍历其子级,调用每个then 的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。这完全取决于你做什么。

    【讨论】:

      【解决方案2】:

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

      KIVY: Carousel inside another carousel

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-05
        • 1970-01-01
        • 2016-09-13
        • 2011-03-09
        相关资源
        最近更新 更多