首页 > 解决方案 > Kivy canvas.clear() 不清除画布

问题描述

我已经按照Kivy 的小部件教程进行操作,但我无法在绘画后清除画布(来自他们的“简单绘画应用程序”教程),除非我在按下清除按钮后调整窗口大小。请帮忙。我正在使用 kivy v1.10.1 和 python v3.7.0。在 Windows 10 64 位操作系统上。此外,当您拖动到新位置时,分散模式中的小部件会留下痕迹,这在调整窗口大小时也会清除。请参见下面的代码:

from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line


class MyPaintWidget(Widget):
    def on_touch_down(self, touch):
        color = (random(), 1, 1)
        with self.canvas:
            Color(*color, mode='hsv')
            d = 30.
            Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
            touch.ud['line'] = Line(points=(touch.x, touch.y))

    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y]


class MyPaintApp(App):
    def build(self):
        parent = Widget()
        self.painter = MyPaintWidget()
        clearbtn = Button(text='Clear')
        clearbtn.bind(on_release=self.clear_canvas)
        parent.add_widget(self.painter)
        parent.add_widget(clearbtn)
        return parent

    def clear_canvas(self, obj):
        self.painter.canvas.clear()


if __name__ == '__main__':
    MyPaintApp().run()

标签: pythonkivykivy-language

解决方案


推荐阅读