python - 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()
解决方案
推荐阅读
- java - 飞溅前的Android白屏
- db2 - 有什么方法可以将 VIP 移到 DB2 TSAMP 环境中的另一个资源组?
- git - git hooks如何通过命令行参数和stdin获取参数?
- python - 输出到 html 时如何增加 jupyter notebook/lab 单元格的宽度
- reactjs - 从数组中删除标签元素也会删除它后面的标签
- django - 从相关的 ForeignKey 获取所有 ManyToManyField 对象
- javascript - 如何将 jQuery Counter 停在上次离开的位置?
- javascript - 点击子菜单后关闭菜单
- json - 如何选择索引 LARAVEL API JSON 的位置
- reactjs - 日历不显示事件,没有错误