Home > Mobile >  Tkinter clear canvas
Tkinter clear canvas

Time:10-06

  • I am using Tkinter and trying to clear a canvas that will display images using the "clear_canvas" method shown below. While the method is accessible and properly triggered via the "clear" button and will reset the add_images_button_count back to zero the canvas.delete("all") line seems to have no affect for some reason. Help would be greatly appreciated.
from tkinter import *
from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk


# GLOBAL VARIABLES
# INITIALIZE WINDOW
home_window = Tk()
# INITIALIZE IMAGE BUTTON COUNT VARIABLE
add_images_button_count = 0
# INITIALIZE X AND Y POSITION FOR IMAGE PLACEMENT
x = 0
y = 0
# INITIALIZE MAX IMAGE COUNT VARIABLE
max_image = []
# CREATE CANVAS
canvas = Canvas(home_window, width=600, height=400, bg="white")
canvas.place(rely=0.1)


# HOME SCREEN
def home_screen():
    # REFERENCE GLOBAL WINDOW
    global home_window

    # SPECIFY WINDOW SIZE
    home_window.geometry("600x450")

    # CREATES TOP FRAME AND DOESN'T ALLOW WINDOW SHRINKAGE
    header_frame = Frame(home_window, width=600, height=50, bd=5, bg="grey")
    header_frame.grid(row=0)

    # BUTTONS
    close_app = Button(header_frame, text="Close App", bg="white", fg="blue", command=home_window.destroy)
    close_app.place(relx=0.015, rely=0.2, anchor="nw")

    back = Button(header_frame, text="Back", bg="white", fg="blue")
    back.place(relx=0.15, rely=0.2, anchor="nw")

    add_images = Button(header_frame, text="Add Images", bg="white", fg="blue", command=add)
    add_images.place(relx=0.45, rely=0.2, anchor="n")

    clear = Button(header_frame, text="Clear", bg="white", fg="blue", command=clear_canvas)
    clear.place(relx=0.55, rely=0.2, anchor="n")

    next_step = Button(header_frame, text="Next Step", bg="blue", fg="white")
    next_step.place(relx=0.985, rely=0.2, anchor="ne")

    # RUN A NON RESIZEABLE TKINTER WINDOW
    home_window.resizable(False, False)
    home_window.mainloop()


# MAX IMAGE WARNING
def max_image_warning():
    global home_window
    pop = Toplevel(home_window)
    pop.geometry("300x200")
    pop.title("Warning")
    Label(pop, text="Max amount of images reached!").pack(pady=20)
    Button(pop, text="Understood", command=pop.destroy).pack(pady=40)


# ADD IMAGES
def add():
    # BUTTON COUNTER
    global home_window, canvas, add_images_button_count, x, y, max_image
    # SPECIFY FILE TYPES
    filetypes = [('JPG Files', '*.jpg'), ('PNG Files', '*.png')]
    # OPEN FILE EXPLORER AND ALLOW USER TO SELECT PHOTO
    filename = askopenfilename(multiple=True, filetypes=filetypes)
    # APPEND NEW FILE TO MAX IMAGE COUNTER
    max_image.append(filename)
    # CHECK IF MAX IMAGE AMOUNT OF 6 IS REACHED, INFORM USER
    if len(max_image) > 6:
        max_image_warning()
        return
    # FIRST CLICK
    if add_images_button_count == 0:
        # START POSITION FOR IMAGE
        x = 0.1
        y = 0.25
        # INCREMENT BUTTON COUNTER
        add_images_button_count  = 1
    # NOT FIRST CLICK
    else:
        # IMAGE POSITIONING LOGIC
        # START NEW ROW AFTER THIRD COLUMN
        if x == 0.7:
            x = 0.1
            y = 0.6
        # ELSE CONTINUE TO INCREMENT COLUMN
        else:
            x  = 0.3
    # MULTIPLE IMAGES METHOD
    for f in filename:
        # OPEN AS IMAGE
        img = Image.open(f)
        # RESIZE IMAGE
        img_resized = img.resize((100, 100))
        # TK PHOTO
        img = ImageTk.PhotoImage(img_resized)
        # CREATE LABEL TO DISPLAY IMAGE
        panel = Label(canvas)
        # IMAGE POSITION
        panel.place(relx=x, rely=y)
        # ASSIGN IMAGE TO LABEL DIRECTLY
        panel.image = img
        # GARBAGE COLLECTION
        panel['image'] = img


# CLEAR
def clear_canvas():
    # REFERENCE GLOBAL VARIABLE
    global canvas, add_images_button_count
    # CLEAR IMAGES
    canvas.delete("all")
    # RESET BUTTON COUNTER TO ZERO
    add_images_button_count = 0


# add_text = tk.Button(window, text="Add Text")
# add_text.grid(row=2, column=2, padx=5, pady=5)
#
# add_logo = tk.Button(window, text="Add Logo")
# add_logo.grid(row=2, column=3, padx=5, pady=5)
#
# undo = tk.Button(window, text="Undo")
# undo.grid(row=2, column=4, padx=5, pady=5)

# RUN HOME SCREEN
home_screen()

CodePudding user response:

canvas.delete("all") only deletes canvas objects -- lines, circles, images, etc that were created with the canvas create methods. You're not using those methods so these images are not canvas objects.

Since you are making the labels children of the canvas, you can iterate over the results of canvas.winfo_children() to destroy the windows:

for child in canvas.winfo_children():
    child.destroy()
  • Related