【问题标题】:KIVY "[CRITICAL] [Clock ] Warning" on_touch_moveKIVY "[CRITICAL] [时钟] 警告" on_touch_move
【发布时间】:2020-04-11 22:58:34
【问题描述】:

我编写了一个小代码来使用 on_touch_move 事件水平拖动小部件。甚至我的小部件水平移动。但是当我拖动小部件时,会生成以下日志。”[CRITICAL] [Clock ] 警告,在下一帧之前完成了太多迭代。检查您的代码,或增加 Clock.max_iteration 属性 em>”。 请在下面找到我的示例代码。要重现这种情况,只需左右拖动白色小部件。上面的日志消息被打印出来。

from kivy.app import App
from kivy.graphics import Rectangle
from kivy.uix.scatter import Scatter
from kivy.uix.relativelayout import RelativeLayout


class MyPaintWidget(Scatter):

    def __init__(self, **kwargs) :
        super(MyPaintWidget, self).__init__(**kwargs)

    def on_touch_move(self, touch):
        touch_x_hint = touch.x / self.parent.size[0]
        self.pos_hint = {'center_x': touch_x_hint }
        return super(Scatter, self).on_touch_move(touch)

class MyPaintApp(App):

    def build(self):
        parent = RelativeLayout()

        Wdgt = MyPaintWidget(pos_hint={'center_x':0.5, 'center_y':0.5}, size_hint=(0.2,0.2))
        with Wdgt.canvas:
            Rectangle(pos_hint = {'center_x':0.5, 'center_y':0.5}, size = (Wdgt.width, Wdgt.height))

        parent.add_widget(Wdgt)
        return parent

if __name__ == '__main__':
    MyPaintApp().run()

【问题讨论】:

    标签: kivy drag


    【解决方案1】:

    我发现您的代码存在两个问题。首先是你的super() 调用MyPaintWidget.on_touch_move() 应该通过MyPaintWidget,而不是Scatter。其次,使用pos_hint会导致额外的计算,所以我建议将你的on_touch_move()更改为:

    def on_touch_move(self, touch):
        self.pos_hint = {'center_y':0.5}  # eliminates the x pos_hint
        self.x = touch.x
        return super(MyPaintWidget, self).on_touch_move(touch)
    

    【讨论】:

    • 谢谢。我仍然需要基于 pos_hint 的解决方案。当我评论 self.pos_hint = {'center_x': touch_x_hint } 不再显示警告。但显然小部件拖动是不可能的。
    • 你是说如果你在我的回答中使用代码你不会被拖累吗?
    • 对不起。您的代码完美运行。如果小部件是使用 pos_hint 创建的,我错误地认为我不应该使用“pos”。您的代码有效:-) 非常感谢
    • 如果您尝试同时使用这两种方法,请务必小心。 pos_hint 将覆盖 pos 的设置。这就是为什么我包含代码self.pos_hint = {'center_y':0.5},它消除了xpos_hint
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 2013-09-26
    相关资源
    最近更新 更多