Reputation: 302
I'm learning kivy and I want to create basic painting app. There's very nice example on kivy's main site. It allows to paint and clear the screen. I just want to add one new thing. I don't want to be allowed to paint "under the button". What I mean by that is I want to limit space on which I'm allowed to paint.
There's documentation code:
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()
It doesn't use .kv file, maybe that is way to go. Thanks for any help.
EDIT: Link to documentation: https://kivy.org/doc/stable/tutorials/firstwidget.html
Upvotes: 0
Views: 428
Reputation: 29488
Ignore touch input that isn't within the bounds you want. For instance, with something like if not self.collide_point(*touch.pos)): return
in your touch handling methods.
You may also want to do something like treat touches as released once they leave the bounds of the drawing area, but that's up to you.
You might also want to use a StencilView to ensure that no drawing can actually appear outside the widget bounds.
Upvotes: 2