使用 Canvas 的 Kivy:开始超出边界时出现行键错误

Kivy using Canvas: Key error with line while starting beyond boundaries

我正在使用 Kivy 创建简单的绘图应用程序。我正在通过这段代码实现绘图:

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

“if”语句用于忽略超出我绘图区域但仍在应用程序内的触摸输入 window。 当我开始在正确的区域绘图时,即使超出边界,一切都正常。

当我在区域外开始绘制并进入区域时出现错误。 这些是我得到的日志:

[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'

感谢帮助:D

错误正如它所说的那样:在触摸开始于小部件边界之外的情况下,您没有将 touch.ud['line'] 设置为任何内容,因此当您尝试访问时该键不存在稍后。

最终你应该编写代码让它做任何你想做的事情(例如你可能想在触摸移动到你的边界时开始一行),但最直接的解决方法是检查 touch.ud['line'] 是否存在在尝试做任何事情之前。