-1

I don't know what to do with the code to erase the lines please help. I am using a tkinter library and PIL.

DEFAULT_PEN_SIZE = 5.0
DEFAULT_COLOR = 'black'

def __init__(self):
    self.root = Tk()

    self.pen_button = Button(self.root, text='pen', command=self.use_pen)
    self.pen_button.grid(row=0, column=0)
  • 1
    If you draw a long line and then use the eraser, do you want to be able to remove the entire line with one click, or do you want to be able to erase only part of a line? – Bryan Oakley Apr 02 '23 at 15:36
  • Following @Bryan Oakley. Increasing the slider scale, then erasing some part of the line. – toyota Supra Apr 02 '23 at 20:35

1 Answers1

1

You are not erasing lines, but creating new white lines actually.

You need to delete lines (not the background image) under the cursor instead:

def __init__(self):
    ...
    # save the background image item ID
    self.bgimg = self.canvas.create_image(0,0,anchor=NW,image=new_image)
    ...

def paint(self, event):
    self.line_width = self.choose_size_button.get()
    if self.eraser_on:
        x1, y1 = event.x-self.line_width/2, event.y-self.line_width/2
        x2, y2 = event.x+self.line_width/2, event.y+self.line_width/2
        # find all items under cursor
        items = self.canvas.find_overlapping(x1, y1, x2, y2)
        # erase items except the background image
        for item in items:
            if item != self.bgimg:
                self.canvas.delete(item)
    else:
        paint_color = self.color
        if self.old_x and self.old_y:
            self.canvas.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)
        self.old_x = event.x
        self.old_y = event.y
acw1668
  • 40,144
  • 5
  • 22
  • 34