0

My PNG file won't show up in my pop up window, despite the variable for my img being global.

global img

def Malwind():
    maltop = Toplevel()
    maltop.iconbitmap('icon.ico')
    maltop.title("Expert Systems Diagnosis Results")
    mgt1 = Label(maltop, text="The breast lump is diagnosed to be: Malignant").grid(row=5, column=2)
    mgt2 = Label(maltop, text="The following symptoms could be observed").grid(row=6, column=2)
    mgt3 = Label(maltop, text="Skin irritation\nPain or tenderness of the nipple\nBloody nipple discharge").grid(row=7, column=2)
    canvas = Canvas(maltop, width = 200, height = 200)
    canvas.grid(row = 1, column = 2)
    img = ImageTk.PhotoImage(Image.open("soft.png"))
    canvas.create_image(0, 0, anchor = NW, image = img)
Auutobot15
  • 47
  • 1
  • 8

1 Answers1

1

The Global keyword is supposed to be within a function and allows the function to modify a variable in the global scope within a function. This means that you have to move global img into the function, and make sure that this variable already exists in the global scope. Here is the corrected code:

def Malwind():
    global img
    maltop = Toplevel()
    maltop.iconbitmap('icon.ico')
    maltop.title("Expert Systems Diagnosis Results")
    mgt1 = Label(maltop, text="The breast lump is diagnosed to be: Malignant").grid(row=5, column=2)
    mgt2 = Label(maltop, text="The following symptoms could be observed").grid(row=6, column=2)
    mgt3 = Label(maltop, text="Skin irritation\nPain or tenderness of the nipple\nBloody nipple discharge").grid(row=7, column=2)
    canvas = Canvas(maltop, width = 200, height = 200)
    canvas.grid(row = 1, column = 2)
    img = ImageTk.PhotoImage(Image.open("soft.png"))
    canvas.create_image(0, 0, anchor = NW, image = img)
DapperDuck
  • 2,728
  • 1
  • 9
  • 21