I'm creating simple drawing app with Kivy. I'm realizing drawing by this code:
class Paint(Widget):
def on_touch_down(self, touch):
color = (random(), 1, 1)
with self.canvas:
Color(*color, mode='hsv')
d = 10.
if touch.y > self.height:
Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
touch.ud['line'] = Line(points=[touch.x, touch.y], width=5)
else:
pass
def on_touch_move(self, touch):
if touch.y > self.height:
touch.ud['line'].points += [touch.x, touch.y]
else:
pass
The "if" statement is for ignoring touch input that is out of my area of drawing but still in-app window. When I start drawing in the right area everything works fine even when I go out of boundaries.
The error occurs when I start drawing outside the area and go into it. These are logs I'm getting:
[INFO ] [Base ] Leaving application in progress... Traceback (most recent call last): File "/home/igor/PycharmProjects/kivytest/kivytest.py", line 46, in MainApp().run() File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/app.py", line 855, in run runTouchApp() File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/base.py", line 504, in runTouchApp EventLoop.window.mainloop() File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/core/window/window_sdl2.py", line 747, in mainloop self._mainloop() File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/core/window/window_sdl2.py", line 479, in _mainloop EventLoop.idle() File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/base.py", line 342, in idle self.dispatch_input() File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/base.py", line 327, in dispatch_input post_dispatch_input(*pop(0)) File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/base.py", line 233, in post_dispatch_input listener.dispatch('on_motion', etype, me) File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/core/window/init.py", line 1404, in on_motion self.dispatch('on_touch_move', me) File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/core/window/init.py", line 1430, in on_touch_move if w.dispatch('on_touch_move', touch): File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "/home/igor/PycharmProjects/kivytest/venv/lib/python3.6/site-packages/kivy/uix/widget.py", line 560, in on_touch_move if child.dispatch('on_touch_move', touch): File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "/home/igor/PycharmProjects/kivytest/kivytest.py", line 30, in on_touch_move touch.ud['line'].points += [touch.x, touch.y] KeyError: 'line'
Thanks for help :D
The error is simply exactly what it says: in the case where the touch starts outside your widget's boundaries, you don't set touch.ud['line']
to anything, therefore that key does not exist when you try to access it later.
Ultimately you should code it to do whatever you want (e.g. you might want to start the line when the touch is moved into your boundaries), but the most straightforward fix would be to check if touch.ud['line']
exists before trying to do anything.