Abhishek Ghanekar
Abhishek Ghanekar

Reputation: 19

Python tkinter erase lines/shapes from the canvas without erasing background image

I am doing a python project where the user can write and draw on a canvas that has a notebook page image as its background. what i want is that the user can erase the drawing on the canvas using mouse movement. I tried using create_line where it paints with white color but this only works if the background is white, but for my background it just looks like the background is getting erased as well.

    def paint(self, event):
    self.line_width = self.choose_size_button.get()
    paint_color = self.color
    if self.old_x and self.old_y:
           self.c.create_line(self.old_x, self.old_y, event.x, event.y,
                           width=self.line_width, fill=paint_color,
                            capstyle=ROUND, smooth=TRUE,splinesteps=36,tags='rub')
    if self.eraser_on :
            self.c.delete(id(self.c.create_line))

    self.old_x = event.x
    self.old_y = event.y

def reset(self, event):
    self.old_x, self.old_y = None, None

I also used event.y event.y in canvas.delete(event.x,event.y) but this does not work as well

Upvotes: 1

Views: 1509

Answers (2)

Stef van der Zon
Stef van der Zon

Reputation: 629

I wrote a small program for you to show what I mend earlier in the comments. Hope it helps.

You need a 640x480 test.png in the same folder as the program and you can run this code. It is just a simple drawing application.

The canvas is a surface to draw on, the screen object is the background.

import pygame as pg
from pygame import Color, Surface

WIDTH = 640
HEIGHT = 480
EMPTY = Color(0,0,0,0) 

screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("Drawing app")
bg = pg.image.load("test.png")
clock = pg.time.Clock()

#I create a transparant canvas
canvas = pg.Surface([640,480], pg.SRCALPHA, 32)

def main():
    is_running = True
    while is_running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                is_running = False
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    is_running = False
            elif event.type == pg.MOUSEMOTION:
                if pg.mouse.get_pressed()[0]:
                    #if mouse 1 is pressed, you draw a circle on the location of the cursor
                    location = (pg.mouse.get_pos())
                    pg.draw.circle(canvas, (0,0,0), location, 20)

            elif event.type == pg.MOUSEBUTTONDOWN:
                #clear canvas on mouse button 3 press
                if event.button == 3:
                    canvas.fill(EMPTY)

        #I blit the background first!
        screen.blit(bg,(0,0))

        #afterwards I overwrite it with a transparant canvas with the drawing I want
        screen.blit(canvas,(0,0))

        pg.display.update()
        clock.tick(200)

if __name__ == "__main__":
    main()

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385880

You cannot erase the way you want with the canvas. The canvas is not a pixel-based painting tool. You can add and remove objects, but you can't paint over or erase just parts of an object.

Upvotes: 1

Related Questions