首页 > 解决方案 > 无法在画布(Tkinter)中显示图像

问题描述

我一直在制作一个画图程序,现在我正在添加一个“打开”功能。这是五分钟前的结果:不注意实物图

不注意画图本身...所以我重新启动了这个东西,这次它只是留下一个空白屏幕,没有显示那样的图像... 结果

这是代码:

def Open():
global Directory
Directory = filedialog.askopenfilename(initialdir="/Desktop", title="Open Image", filetypes=(("Portable Network Graphics","*.png"),("Joint Photographic Experts Group","*.jpg"),("all files","*.*")))

ImageOpened = Image.open(Directory)
Largeur, Hauteur = ImageOpened.size

if Largeur >= 1000 or Hauteur >= 1000:
    messagebox.showwarning("Can't open image", "The image is too big!")
elif not Largeur >= 1000 or not Hauteur >= 1000:
    Can.delete(ALL)
    FinalImage = ImageTk.PhotoImage(ImageOpened)
    Can.configure(width=Largeur, height=Hauteur)

    WidthPosition = Largeur/2
    WidthPosition = WidthPosition+2

    HeightPosition = Hauteur/2
    HeightPosition = HeightPosition+2

    print (Largeur, Hauteur, WidthPosition, HeightPosition)
    Can.create_image(WidthPosition,HeightPosition, image=FinalImage)

有人可以帮我吗?;-;

标签: pythonimagecanvastkinterpython-imaging-library

解决方案


刚刚解决了,一开始我忘记了global... ^^'所以代码现在看起来像这样:

def Open():
global Directory, FinalImage
Directory = filedialog.askopenfilename(initialdir="/Desktop", title="Open Image", filetypes=(("Portable Network Graphics","*.png"),("Joint Photographic Experts Group","*.jpg"),("all files","*.*")))

ImageOpened = Image.open(Directory)
Largeur, Hauteur = ImageOpened.size

if Largeur >= 1000 or Hauteur >= 1000:
    messagebox.showwarning("Can't open image", "The image is too big!")
elif not Largeur >= 1000 or not Hauteur >= 1000:
    Can.delete(ALL)
    FinalImage = ImageTk.PhotoImage(ImageOpened)
    Can.configure(width=Largeur, height=Hauteur)

    WidthPosition = Largeur/2
    WidthPosition = WidthPosition+2

    HeightPosition = Hauteur/2
    HeightPosition = HeightPosition+2

    print (Largeur, Hauteur, WidthPosition, HeightPosition)
    Can.create_image(WidthPosition,HeightPosition, image=FinalImage)

推荐阅读