前提
PythonのGUIライブラリKivyを使ってペイントアプリを作ろうとしています。
キャンバスに画像を表示し、その上からお絵描きができるようなものです。
実現したいこと
スライダーで画像を拡大・縮小できるようにしたのですが、その上に描いた線や絵も拡大・縮小できるようにしたいです。将来的には画像を回転や移動をさせた時にも一緒に動くようにしたいです。
発生している問題・エラーメッセージ
画像を拡大・縮小しても線や絵が同じように拡大・縮小されない。別々のキャンバスに描画されているため?
該当のソースコード
Python
1from kivy.app import App 2from kivy.uix.widget import Widget 3from kivy.uix.boxlayout import BoxLayout 4from kivy.uix.anchorlayout import AnchorLayout 5from kivy.uix.button import Button 6from kivy.graphics import Color, Line 7from kivy.uix.slider import Slider 8from kivy.uix.image import Image 9from kivy.lang import Builder 10 11 12Builder.load_string(""" 13#:kivy 2.1.0 14<MyLayout>: 15 BoxLayout: 16 size: root.size 17 orientation: "vertical" 18 19 AnchorLayout: 20 size_hint: 1, 0.8 21 22 MyPaintWidget: 23 id: paint_area 24 size_hint: sl.value, sl.value 25 source: "test.jpg" 26 27 Slider: 28 size_hint: 1, 0.1 29 id: sl 30 min: 0 31 max: 2 32 step: 0.01 33 orientation: "horizontal" 34 value: 1 35 36 Button: 37 size_hint: 1, 0.1 38 text: "Clear" 39 on_press: root.clear_canvas() 40""") 41 42 43class MyPaintWidget(Image): 44 def __init__(self, **kwargs): 45 super(MyPaintWidget, self).__init__(**kwargs) 46 47 def on_touch_down(self, touch): 48 color = (1, 0, 0) 49 with self.canvas.after: 50 Color(*color) 51 touch.ud['line'] = Line(points=(touch.x, touch.y), width = 3) 52 53 def on_touch_move(self, touch): 54 try: 55 touch.ud['line'].points += [touch.x, touch.y] 56 except Exception as e: 57 pass 58 59 60class MyLayout(BoxLayout): 61 def __init__(self, **kwargs): 62 super(MyLayout, self).__init__(**kwargs) 63 64 def clear_canvas(self): 65 self.ids.paint_area.canvas.after.clear() 66 67 68class MyPaintApp(App): 69 def __init__(self, **kwargs): 70 super(MyPaintApp, self).__init__(**kwargs) 71 72 def build(self): 73 return MyLayout() 74 75 76if __name__ == '__main__': 77 MyPaintApp().run()
試したこと
canvasにはbeforeとafterを含めて3種類あるようで、切り替えてみたのですがうまくいきませんでした。
補足情報(FW/ツールのバージョンなど)
Python 3.8.2
Kivy 2.1.0
初めての質問になります。Kivyは日本語の情報が少ない感じで勉強に苦労しています。
よろしくお願いします。
あなたの回答
tips
プレビュー