Use function to clear Tkinter image created from a different function

Viewed 47

I am new to python and am building a simple Tkinter GUI. In one function I display an image from a url from JSON using the following code.

def show_card_image(url_edited):
    raw_data = urllib.request.urlopen(url_edited).read()
    im = Image.open(io.BytesIO(raw_data))
    card_image = ImageTk.PhotoImage(im)

    return card_image

The card image is then added to a Tkinter label in another function that populates the GUI with the card image and other JSON data. The portion that adds the card is shown below.

card_image = show_card_image(url_edited)

    # image label in window
    image_label = tk.Label(root, image=card_image)
    image_label.image = card_image
    image_label.grid(row=1, column=1)

    return image_label

I then have another function that clears other fields populated from the JSON data. I would also like that clear function to remove the image displayed above. The clear function is shown below.

# function to clear fields
def clear_fields(image_label):

    name_entry.delete(0, tk.END)
    type_entry.delete(0, tk.END)
    set_name.delete(0, tk.END)
    mana_cost.delete(0, tk.END)
    price_text.delete(0, tk.END)
    rules_text.delete("1.0", tk.END)
    flavor_text.delete("1.0", tk.END)
    image_label.destroy()

    return None 

When I hit the button to run the clear_fields function. The other JSON text fields are cleared but the image is not and the following error appears in the console.

TypeError: clear_fields() missing 1 required positional argument: 'image_label'

This is likely a simple thing and I have read bunch of articles on clearing Tkinter images but have yet to figure this out.

Any suggestions for how to solve this?

Thanks!

0 Answers
Related