如何更改画布中的图像?[python tkinter] [英] How to change image in canvas?[python tkinter]

查看:82
本文介绍了如何更改画布中的图像?[python tkinter]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下所示,我想使用tkinter在画布上显示图像,并且当单击按钮时,应该显示另一张照片。但是我失败了。第一张图片显示得很好,但是当我单击按钮时,图片没有改变

As below, I want to show an image in canvas using tkinter, and when a click the button, an other photo should be shown. But I failed. The first image shows well, but the image didn't change when I click the button

C = Tkinter.Canvas(top, bg="#fff", height=500, width=600)

// show image1 in canvas first and it works
itk = ImageTk.PhotoImage(img1)
C.create_image(300, 250, image=itk)
C.pack()


def changeImage():
    // I want to show image2 in canvas, but I fails
    print 'change image in canvas'
    itk2 = ImageTk.PhotoImage(img2)
    C.create_image(300, 250, image=itk2)

button = Tkinter.Button(top,text='click', command=changeImage)
button.pack()


top.mainloop()


推荐答案

更改所有匹配项的一个或多个选项。 1

Changes one or more options for all matching items. 1

myimg = C.create_image(300, 250, image=itk)

def changeImage():
    // I want to show image2 in canvas, but I fails
    print 'change image in canvas'
    itk2 = ImageTk.PhotoImage(img2)
    C.itemconfigure(myimg, image=itk2)

这篇关于如何更改画布中的图像?[python tkinter]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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