tkinter画布上的图像显示不工作 [英] Image display on tkinter canvas not working

查看:796
本文介绍了tkinter画布上的图像显示不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个简单的绘图程序,我有完全功能的保存和绘制功能,虽然在打开按钮的方法,msg.create_image函数不显示图像。这是没有错误,只是GUI和一个空白的大画布。
这里是我的代码在python 2.7.5与PIL 1.1.7写在Windows 7 64位,并正确运行在Windows和Mac上运行OS X山狮,10.9
PS我道歉身份...他们可能由于将其复制到此帖子而关闭。
感谢

I am trying to write a simple paint program and I have fully functioning save and draw features, though in the open button's method, the msg.create_image function isnt showing the image. THere is no error, just the GUI and a blank large canvas. Here is my code in python 2.7.5 with PIL 1.1.7 written on a windows 7 64 bit and run correctly on both the windows and a Mac running OS X Mountain Lion, 10.9 PS I apologize for identations... They may be off due to copying it to this post. Thanks

推荐答案

如果在函数中创建ImageTk.PhotoImage并将其分配给局部变量,收集器当你离开那个函数。

If you create ImageTk.PhotoImage in function and you assign it to local variable it is destroyed by garbage collector when you leave that function. And that is why you do not see the image.

您必须将其指定给全局变量

You have to assign it to global variable

tk_im = None

def opens():
    global img,msg,idraw

    global tk_im

    filename = askopenfilename()
    im = Image.open(filename)
    tk_im = ImageTk.PhotoImage(im)
    a = msg.create_image(0,0,image=tk_im)
    idraw = ImageDraw.Draw(im)

如果您要添加更多图片

tk_im = []

def opens():
    global img,msg,idraw

    global tk_im

    filename = askopenfilename()
    im = Image.open(filename)

    tk_im.append( ImageTk.PhotoImage(im) )

    a = msg.create_image(0,0, image=tk_im[-1] )
    idraw = ImageDraw.Draw(im)

这篇关于tkinter画布上的图像显示不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆