minTwin
minTwin

Reputation: 1393

How to add a clear button for a python kivy painting app

I am building a basic painting app with python and kivy for learning app development. I am trying to figure out how to clear the canvas after drawing on the screen by modifying the code on the .kv file.

Within the .kv file I think I need to modify the #on_release:root.canvas.clear() portion of the code, currently it deletes the entire canvas and buttons. I am trying to figure how to make it so that it just clears the screen only, allows to redraw on the screen again, and does not erase the buttons.

Heres a picture for context Painter App

What happens after pressing the clear button Blank Screen

from random import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.graphics import Line, Color, Ellipse

class Painter(Widget):
    def on_touch_down(self, touch):
    color = (random(), 1.,1.) #reduce number of possible colors
    with self.canvas:
        Color(*color, mode='hsv') #sets the colors to be equally bright
        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 MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("main3.kv") #load the kivy file

class SimpleKivy7(App):
    def build(self):
        return presentation

if __name__== "__main__":
    SimpleKivy7().run()
The following is the kivy file
#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>:
    name: "main"
    Button:
        on_release: app.root.current = "other"
        text: "Next Screen"
        font_size: 50

<AnotherScreen>:
    name: "other"

FloatLayout:
    Painter
    Button:
        color:0,1,0,1
        font_size: 25
        size_hint: 0.3, 0.2
        text: "Back Home"
        on_release: app.root.current = "main"
        pos_hint: {"right":1, "top":1}

    Button:
        color:0,1,0,1
        font_size: 25
        size_hint: 0.3, 0.2
        text: "Clear"
        #on_release:root.canvas.clear() #root.canvas.clear() clears everything. 
        pos_hint: {"left":1, "top":1}

Upvotes: 1

Views: 1512

Answers (1)

Edvardas Dlugauskas
Edvardas Dlugauskas

Reputation: 1489

I assume that the code you provided is just a part of your codebase. I encountered some errors that I would like to clear up: there was no indentation from line 10 after the Painter def on_touch_down, I indented lines 10-17 once. In the .kv file there were two widgets kivy considered as root widgets (there can only be one). I added < and > around your screen manager ScreenManagement on line 3. I am also sorry to say that in my case the app only draws the circles, but not the lines after them.

The problem lies on line 34 in the .kv file: on_release: root.canvas.clear(). The root refers to the FloatLayout. So you're clearing all that is on the FloatLayout including the buttons. What you need to clear is the Painter widget's canvas. Add an id to your Painter and clear its canvas instead:

FloatLayout:
    Painter:
        id: painter # an id for referring to this widget
    Button:
        color:0,1,0,1
        font_size: 25
        size_hint: 0.3, 0.2
        text: "Back Home"
        on_release: app.root.current = "main"
        pos_hint: {"right":1, "top":1}

    Button:
        color:0,1,0,1
        font_size: 25
        size_hint: 0.3, 0.2
        text: "Clear"
        on_release:  painter.canvas.clear() # clear the Painter's canvas
        pos_hint: {"left":1, "top":1}

Upvotes: 6

Related Questions