图像未显示在python中

时间:2016-03-30 05:57:21

标签: image python-2.7 tkinter

我在窗口中为显示图像编写以下代码,但不显示图像。只出现空白窗口。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from Tkinter import *
import matplotlib, sys
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib import pylab as plt
root=Tk()
fr=Frame(root)
fr.pack()
image = mpimg.imread("C:\Users\Public\Pictures\SamplePictures\Koala.jpg")
f = Figure(figsize=(5,5), dpi=100)  
im=plt.imshow(image)
canvas = FigureCanvasTkAgg(f, fr)
canvas.show()
canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

以上问题有解决方案在画布中显示图像。我们需要更换 f =图(figsize =(5,5),dpi = 100)到f = plt.figure(figsize =(5,5),dpi = 100)这一行