Re: [kivy-users] change line color on button press

365 views
Skip to first unread message

Oon-Ee Ng

unread,
Feb 7, 2016, 5:52:17 PM2/7/16
to kivy-...@googlegroups.com
The canvas is a list of drawing instructions. You're responsible for
keeping track of what's been drawn (and clearing it). You're already
doing the former, but not the latter.

On Mon, Feb 8, 2016 at 1:25 AM, Ameya Nanivadekar <acn...@gmail.com> wrote:
> I'm playing around with the simple paint app example and I was wondering if
> there is a way to change the color of individual lines after they have been
> drawn. My current implementation is: I draw a line and save the pixel
> coordinates, when I press a button I set the canvas color and redraw the
> line using the saved pixel coordinates. Do I have to redraw every time I
> want to change the properties of a line? I couldn't figure out if there is a
> line object or how it can be accessed.
>
> from kivy.app import App
> from kivy.uix.widget import Widget
> from kivy.uix.button import Button
> from kivy.graphics import Color, Ellipse, Line
> from kivy.properties import ListProperty, BooleanProperty
>
>
>
> class MyPaintWidget(Widget):
> pixelList=ListProperty([])
> colorChanged=BooleanProperty('False')
>
> def on_touch_down(self, touch):
> color = (1, 1, 1)
> with self.canvas:
> Color(*color)
> d = 30.
> touch.ud['line'] = Line(points=(touch.x, touch.y))
>
> def on_touch_move(self, touch):
> touch.ud['line'].points += [touch.x, touch.y]
>
> def on_touch_up(self, touch):
> if self.colorChanged:
> self.pixelList=touch.ud['line'].points
> self.colorChanged=not self.colorChanged
>
> def colorChange(self):
> self.canvas.clear()
> with self.canvas:
> Color(0,1,0)
> Line(points=(self.pixelList))
>
> class MyPaintApp(App):
>
> def build(self):
> parent = Widget()
> self.painter = MyPaintWidget()
> #clearbtn = Button(text='Clear')
> #clearbtn.bind(on_release=self.clear_canvas)
>
> btn2 = Button(text='change color', pos=(100,0))
> btn2.bind(on_press=lambda a:self.painter.colorChange())
>
> parent.add_widget(self.painter)
> #parent.add_widget(clearbtn)
> parent.add_widget(btn2)
>
> return parent
>
> def clear_canvas(self, obj):
> self.painter.canvas.clear()
>
>
> if __name__ == '__main__':
> MyPaintApp().run()
>
> --
> You received this message because you are subscribed to the Google Groups
> "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to kivy-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages