pythontkinter

Script works with error line but won't run corectly when bad line is removed


I have the following code that will error on line 27. I don't need Line 27(photo = ImageTk.PhotoImage(img)). However the program works as expected even after hitting the error. if I delete this line the images won't show I can not figure out why

import tkinter as tk
from PIL import Image, ImageTk
import string # string.ascii_lowercase = "abcdefghijklmnopqrstuvwxyz"

def on_enter(event):
    global image_label
    btn=str(event.widget)

    match btn:
        case ".1":
            img = Image.open("District_1b.png")
        case ".2":
            img = Image.open("District_2b.png")
        case ".3":
            img = Image.open("District_3b.png")
        case ".4":
            img = Image.open("District_4b.png")            
        case ".5":
            img = Image.open("District_5b.png")            
        case ".6":
            img = Image.open("District_6b.png")            
        case ".7":
            img = Image.open("Districtsb.png")

    img = ImageTk.PhotoImage(img)
    image_label.configure(image=img)
    photo = ImageTk.PhotoImage(img)
    
def on_leave(event):
    global image_label
    #image_label.destroy()

root = tk.Tk()

button1 = tk.Button(root, text="District I", name="1")
button1.grid(row=0,column=0)
button2 = tk.Button(root, text="District II", name="2")
button2.grid(row=0,column=1)
button3 = tk.Button(root, text="District III", name="3")
button3.grid(row=0,column=2)
button4 = tk.Button(root, text="District IV", name="4")
button4.grid(row=0,column=3)
button5 = tk.Button(root, text="District V", name="5")
button5.grid(row=0,column=4)
button6 = tk.Button(root, text="District VI", name="6")
button6.grid(row=0,column=5)
button7 = tk.Button(root, text="All District's", name="7")
button7.grid(row=0,column=6)

img = Image.open("Screenshot 2024-12-13 144137.png")
img = ImageTk.PhotoImage(img)
image_label = tk.Label(root,width=1400, image=img)
image_label.grid(row=1,column=0, columnspan=8)

button1.bind("<Enter>", on_enter)
button1.bind("<Leave>", on_leave)
button2.bind("<Enter>", on_enter)
button2.bind("<Leave>", on_leave)
button3.bind("<Enter>", on_enter)
button3.bind("<Leave>", on_leave)
button4.bind("<Enter>", on_enter)
button4.bind("<Leave>", on_leave)
button5.bind("<Enter>", on_enter)
button5.bind("<Leave>", on_leave)
button6.bind("<Enter>", on_enter)
button6.bind("<Leave>", on_leave)
button7.bind("<Enter>", on_enter)
button7.bind("<Leave>", on_leave)

root.mainloop()

I know PhotoImage' object has no attribute ' However again if i comment this out or simply delete it. The image just shows a blank area. if the error is left in I can hover each button and the image will update as expected even though the error has been hit.


Solution

  • Line 27 (photo = ImageTk.PhotoImage(img)) is unnecessary and should be removed.

    img is a local variable, and Python's garbage collector removes it after the function exits. As a result, the image disappears from image_label.

    The solution is to store the image as a global variable or an attribute so that it persists.