无法在Canvas(Tkinter)中显示图像

时间:2018-11-07 06:22:07

标签: python image canvas tkinter python-imaging-library

我一直在制作Paint程序,现在我添加了“打开”功能。这是五分钟前的结果:Don't pay attention to the actual drawing

不要关注绘图本身...所以我重新启动了绘图,这次只是留下了一个空白屏幕,而没有显示这样的图像... The result

代码如下:

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)

有人可以帮我吗? ;-;

1 个答案:

答案 0 :(得分:0)

就解决了,我在开始时忘记了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)