我已经定义了一个菜单,它应该在一个项目中显示一个图像(JPG,250x250)。
import Tkinter
from PIL import Image, ImageTk
def show_photo():
img=Image.open("image.jpg")
photo=ImageTk.PhotoImage(img)
width=photo.width();
height=photo.height()
w=Tkinter.Toplevel(root)
w.geometry(str(width+2)+'x'+str(height+2))
w.title(str(width)+'x'+str(height))
canvas=Tkinter.Canvas(w, bg="black", width=width, height=height)
canvas.pack()
img=canvas.create_image(0,0,anchor=Tkinter.NW,image=photo)
xxx # NameError
root=Tkinter.Tk()
mainmenu = Tkinter.Menu(root)
menu1 = Tkinter.Menu(mainmenu)
mainmenu.add_cascade(label="Menu 1", menu=menu1)
menu1.add_command(label="Show photo", command=show_photo)
root.config(menu=mainmenu)
root.mainloop()
除非我放错线(这里:xxx),否则图像不会出现,只有正确大小的黑色矩形。
有什么线索吗?
预先感谢,埃里克。