我编写了一个小代码,使用on_touch_move事件将其水平拖动。甚至我的小部件都水平移动。但是,当我拖动窗口小部件时,将生成以下日志。“ [[CRITICAL] [Clock]警告,在下一帧之前进行了太多的迭代。请检查代码,或增加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()
我看到您的代码有两个问题。首先是您在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)