KIVY“[严重] [时钟]警告”on_touch_move

KIVY "[CRITICAL] [Clock ] Warning" on_touch_move

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

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()

我发现您的代码有两个问题。首先是您在 MyPaintWidget.on_touch_move() 中的 super() 调用应该通过 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)