2
我正在嘗試重構the last code sample以便Button
實際上是其自己的類,並且具有on_release
操作。但是我的代碼失敗了。將一個on_release動作添加到一個kivy按鈕
我想不僅重構它(每下面我嘗試),但我還需要設置Button
爲「清除」
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): userdata = touch.ud userdata['color'] = c = (random(), 1, 1) with self.canvas: Color(*c, mode='hsv') d = 30 Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d)) userdata['line'] = Line(points=(touch.x, touch.y)) def on_touch_move(self, touch): touch.ud['line'].points += [touch.x, touch.y] class ClearButton(Button): def __init__(self, paint_widget): self.paint_widget=paint_widget def on_release(self, button): self.paint_widget.canvas.clear() class MyPaintApp(App): def build(self): parent = Widget() painter = MyPaintWidget() clearbtn = ClearButton(painter) parent.add_widget(painter) parent.add_widget(clearbtn) return parent if __name__ == '__main__': MyPaintApp().run()