尽管img的变量是全局变量,但我的PNG文件不会显示在弹出窗口中。
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)
所述Global
关键字被认为是一个函数内,并允许在函数的函数中的全局范围修改变量的值。这意味着您必须global img
进入该函数,并确保此变量已存在于全局范围中。这是更正的代码:
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句