0

I have written a small code to drag widget horizontally using on_touch_move event. Even my widget moves horizontally. But when I am dragging the widget, following log gets generated."[CRITICAL] [Clock ] Warning, too much iteration done before the next frame. Check your code, or increase the Clock.max_iteration attribute". Please find below my sample code. To reproduce this scenario, just drag the white widget right and left. The above log messages gets printed.

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()
KJG
  • 93
  • 9

1 Answers1

1

I see two problems with your code. The first is that your super() call in MyPaintWidget.on_touch_move() should pass MyPaintWidget, not Scatter. Second, using pos_hint causes extra calculations, so I suggest changing your on_touch_move() to:

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)
John Anderson
  • 35,991
  • 4
  • 13
  • 36
  • Thanks. I still need solution based on pos_hint. When I commented self.pos_hint = {'center_x': touch_x_hint } no more warning is displayed. But obviously widget drag is not possible. – KJG Apr 11 '20 at 17:41
  • Are you saying that you don't get drag if you use the code in my answer? – John Anderson Apr 11 '20 at 17:44
  • Sorry. Your code works perfect. I wrong assumed that I should not use 'pos' if widgets are created using pos_hint. Your code works :-) Thanks a lot – KJG Apr 11 '20 at 18:11
  • You must be careful if you try using both. The `pos_hint` will over-ride settings for `pos`. That is why I included the code `self.pos_hint = {'center_y':0.5}` which eliminates the `pos_hint` for `x`. – John Anderson Apr 11 '20 at 18:16