不显示tkinter画布中的图像

时间:2016-04-04 19:52:39

标签: python-2.7 button tkinter tkinter-canvas

我想通过按钮命令在画布中显示图像,但它不起作用!

这是代码:

from Tkinter import Tk,Button,Canvas,PhotoImage
from PIL import Image, ImageTk



def plot ():




    image = Image.open("img1.png")
    image = image.resize( (400,300), Image.ANTIALIAS) 
    img = ImageTk.PhotoImage(image)
    canvas.create_image(200, 175, image = img)



    return 0

tkk = Tk()
canvas = Canvas(tkk, \
width =400, height =650, \
bg="light yellow")
canvas.pack(side ="right")
button_plot = Button(tkk, text = "dessiner", command = dessiner, width = 50, fg = "red")
button_plot.pack()
mw.mainloop()

我试图在我的函数'plot()'中使用:TopLevel()但是我得到的结果是没有任何图像的画布!

1 个答案:

答案 0 :(得分:0)

最后,我找到了在函数plot()的末尾添加此命令的解决方案:

  import matplotlib.pyplot as plt

  canvas.create_image(200, 175, image = img)
  plt.show()