質問編集履歴

1

コードの改善して見やすくした

2022/12/08 10:48

投稿

payayan
payayan

スコア0

test CHANGED
File without changes
test CHANGED
@@ -1,30 +1,25 @@
1
1
  ### 前提
2
2
 
3
3
  PythonのGUIライブラリKivyを使ってペイントアプリを作ろうとしています。
4
- キャンバスに画像(写真)を表示し、その上からお絵描きができるようなものです。
4
+ キャンバスに画像を表示し、その上からお絵描きができるようなものです。
5
5
 
6
6
  ### 実現したいこと
7
7
 
8
- スライダーで画像(写真)を拡大・縮小できるようにしたのですが、その上に描いた線や絵も拡大・縮小できるようにしたいです。将来的には画像を回転や移動をさせた時にも一緒に動くようにしたいです。
8
+ スライダーで画像を拡大・縮小できるようにしたのですが、その上に描いた線や絵も拡大・縮小できるようにしたいです。将来的には画像を回転や移動をさせた時にも一緒に動くようにしたいです。
9
9
 
10
10
  ### 発生している問題・エラーメッセージ
11
11
 
12
- 画像(写真)を拡大・縮小しても線や絵が同じように拡大・縮小されない。別々のキャンバスに描画されているため?
12
+ 画像を拡大・縮小しても線や絵が同じように拡大・縮小されない。別々のキャンバスに描画されているため?
13
13
 
14
14
  ### 該当のソースコード
15
15
 
16
16
  ```Python 3.8.2
17
17
  from kivy.app import App
18
- from kivy.config import Config
19
-
20
- Config.set('input', 'mouse', 'mouse, disable_multitouch')
21
-
22
18
  from kivy.uix.widget import Widget
23
19
  from kivy.uix.boxlayout import BoxLayout
24
20
  from kivy.uix.anchorlayout import AnchorLayout
25
21
  from kivy.uix.button import Button
26
22
  from kivy.graphics import Color, Line
27
- from kivy.core.window import Window
28
23
  from kivy.uix.slider import Slider
29
24
  from kivy.uix.image import Image
30
25
  from kivy.lang import Builder
@@ -61,39 +56,26 @@
61
56
  """)
62
57
 
63
58
 
64
- Window.size = (800, 600)
65
-
66
-
67
59
  class MyPaintWidget(Image):
68
60
  def __init__(self, **kwargs):
69
61
  super(MyPaintWidget, self).__init__(**kwargs)
70
-
71
62
 
72
63
  def on_touch_down(self, touch):
73
64
  color = (1, 0, 0)
74
65
  with self.canvas.after:
75
66
  Color(*color)
76
-
77
- if not self.collide_point(*touch.pos):
78
- return
79
-
80
67
  touch.ud['line'] = Line(points=(touch.x, touch.y), width = 3)
81
68
 
82
-
83
69
  def on_touch_move(self, touch):
84
- if not self.collide_point(*touch.pos):
85
- return
86
-
87
70
  try:
88
71
  touch.ud['line'].points += [touch.x, touch.y]
89
- except:
72
+ except Exception as e:
90
73
  pass
91
74
 
92
75
 
93
76
  class MyLayout(BoxLayout):
94
77
  def __init__(self, **kwargs):
95
78
  super(MyLayout, self).__init__(**kwargs)
96
-
97
79
 
98
80
  def clear_canvas(self):
99
81
  self.ids.paint_area.canvas.after.clear()
@@ -102,7 +84,6 @@
102
84
  class MyPaintApp(App):
103
85
  def __init__(self, **kwargs):
104
86
  super(MyPaintApp, self).__init__(**kwargs)
105
-
106
87
 
107
88
  def build(self):
108
89
  return MyLayout()