Python Tkinter.jpeg图像未显示--错误

Python Tkinter.jpeg图像未显示--错误,python,image,class,tkinter,canopy,Python,Image,Class,Tkinter,Canopy,我正在尝试将一个.jpeg图像添加到我的GUI中,同时它还有几个其他小部件 使用我的代码,当我使用以下命令时,我可以在单独的Tkinter窗口中显示图像: #self.label=label(image=self.img) 但是,每当我尝试将图像添加到原始的Tkinter窗口时,我都会看到代码下面的错误。我尝试将其添加到原始Tkinter窗口的方式是: #self.label=label(frame,image=self.img) 复制错误 奇怪的是,当我尝试在较短版本的代码(如下面的代码)中复

我正在尝试将一个
.jpeg
图像添加到我的GUI中,同时它还有几个其他小部件

使用我的代码,当我使用以下命令时,我可以在单独的
Tkinter
窗口中显示图像:

#self.label=label(image=self.img)

但是,每当我尝试将图像添加到原始的
Tkinter
窗口时,我都会看到代码下面的错误。我尝试将其添加到原始Tkinter窗口的方式是:

#self.label=label(frame,image=self.img)

复制错误 奇怪的是,当我尝试在较短版本的代码(如下面的代码)中复制错误时,它是有效的。然而!要在缩短的代码中复制错误,需要首先创建不同的错误。示例:将
text=“Try”
替换为
text=“%s”%yikes
(因为没有变量
yikes
,它将给您一个错误)。将代码更改回原来的方式后,它会产生我在下面描述的错误(
TclError:image“pyimage\uuuuuuuuuu”不退出
)。在最底层,我包括了整个班级,因为我很难持续地复制这个问题。我正在使用Python 2.7和Canopy 1.5.5

缩短代码:
导入matplotlib.pyplot作为plt
从Tkinter进口*
从PIL导入ImageTk,图像
从matplotlib.backends.backend_tkagg导入图CAVASTKAGG
从matplotlib.figure导入图形
将numpy作为np导入
从tkFileDialog导入askopenfilename、askdirectory
类应用程序:
定义初始(自我,主):
帧=帧(主帧)
self.button_left=按钮(框,text=“<上一个事件”)
self.button_left.grid(行=1,列=0)
self.button\u right=按钮(frame,text=“Next Event>”)
self.button\u right.grid(行=1,列=3)
#为指示泄漏事件数量的UI创建文本
w=标签(框,text=“尝试重新创建错误”)
w、 网格(行=1,列=2)
self.m=画布(框架,宽度=50,高度=25)
self.text\u id=self.m.create\u text(25,12.5,text=“Try”)
self.m.grid(行=1,列=1)
self.path=“C:\Carbonite\EL_36604.02_231694\EL_36604.02_231694_2015-06-15 10.39.57.jpeg”
self.image=image.open(self.path)
self.img=ImageTk.PhotoImage(self.image)
#self.label=label(image=self.img)
self.label=label(框架,图像=self.img)
self.label.image=self.img
self.label.grid(行=3,列=0)
frame.grid(行=0,列=0)
root=Tk()
app=app(根目录)
root.mainloop()
当我使用注释掉的方法时,我在程序中收到错误:
tcl错误回溯(最近一次调用)
C:\Carbonite\Main\u interface\u file.py in()
136
137根=Tk()
-->138应用程序=应用程序(根)
139 root.mainloop()
140
C:\Carbonite\Main\u interface\u file.py in\uuuuu init\uuuu(self,master)
72 self.img=ImageTk.PhotoImage(self.image)
73#self.label=label(image=self.img)
--->74 self.label=label(框架,图像=self.img)
75 self.label.image=self.img
76 self.label.grid(行=3,列=0)
C:\Users\U10596\AppData\Local\enthught\corporation\App\AppData\corporation-1.5.5.3123.win-x86\u 64\lib\lib-tk\Tkinter.pyc在uuuu init\uuu中(self、master、cnf、**kw)
2585
2586         """
->2587小部件。\uuuuu初始\uuuuuuuuuuuuu(自我,主,“标签”,cnf,kw)
2588
2589类列表框(小部件,XView,YView):
C:\Users\U10596\AppData\Local\enthught\corporation\App\AppData\corporation-1.5.5.3123.win-x86\u 64\lib\lib tk\Tkinter.pyc in uuuuu init\uuuu(self、master、widgetName、cnf、kw、extra)
2084德尔cnf[k]
2085自助电话(
->2086(widgetName,self._w)+额外+自选项(cnf))
2087对于k,v类:
2088 k.configure(自我,v)
Tcl错误:映像“pyimage8”不存在
几乎全部代码:
导入matplotlib.pyplot作为plt
从Tkinter进口*
从PIL导入ImageTk,图像
从matplotlib.backends.backend_tkagg导入图CAVASTKAGG
从matplotlib.figure导入图形
将numpy作为np导入
从图像\u到\u列表导入图像\u到\u列表
从tkFileDialog导入askopenfilename、askdirectory
#创建创建UI的类
类应用程序:
定义初始(自我,主):
self.event_num=1
#创建一个容器
帧=帧(主帧)
#创建2个按钮(泄漏事件之间的更改
self.button_left=按钮(框,text=“”,
命令=自我增加)
self.button\u right.grid(行=1,列=3)
#为指示泄漏事件数量的UI创建文本
w=标签(帧,文本=“/%s”%len(tft))
w、 网格(行=1,列=2)
#显示系列中当前事件的编号
self.m=画布(框架,宽度=50,高度=25)
self.text_id=self.m.create_text(25,12.5,text=“%s”%(self.event_num+1))
self.m.grid(行=1,列=1)
#创建电压数据的绘图
self.fig=图()
self.ax=self.fig.add_子批次(111)
self.fig.autofmt_xdate()
将matplotlib.dates导入为mdates
self.ax.fmt_xdata=mdates.DateFormatter(“%Y-%m-%d”)
self.line,=self.ax.plot(tft[self.event\u num],tf1[self.event\u num],'。)
self.line2,=self.ax.plot(tft[self.event\u num],tf2[self.event\u num],'。)
self.ax.set_ylim([0,3.5])
self.path=“C:\Carbonite\EL_36604.02_231694\EL_36604.02_231694_2015-06-15 10.39.57.jpeg”
self.image=image.open(self.path)
self.img=ImageTk.PhotoImage(self.image)
#self.label=label(image=self.img)
self.label=标签(框架,图像=se
import matplotlib.pyplot as plt
from Tkinter import *
from PIL import ImageTk, Image
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
from tkFileDialog import askopenfilename, askdirectory

class App:

    def __init__(self, master):
        frame = Frame(master)

        self.button_left = Button(frame,text="< Previous Event")
        self.button_left.grid(row=1,column=0)
        self.button_right = Button(frame,text="Next Event >")
        self.button_right.grid(row=1,column=3)

       #Creating text for the UI indicating the number of leakage events
        w = Label(frame, text="Trying to Recreate error")
        w.grid(row=1,column=2)

        self.m = Canvas(frame,width=50,height=25)
        self.text_id = self.m.create_text(25,12.5, text="Try")
        self.m.grid(row=1,column=1)


        self.path = "C:\Carbonite\EL_36604.02_231694\EL_36604.02_231694_2015-06-15 10.39.57.jpeg"
        self.image = Image.open(self.path)
        self.img = ImageTk.PhotoImage(self.image)
        #self.label = Label(image = self.img)
        self.label = Label(frame,image = self.img)
        self.label.image = self.img
        self.label.grid(row = 3, column = 0)
        frame.grid(row=0,column=0)

root = Tk()
app = App(root)
root.mainloop()
    TclError                                  Traceback (most recent call last)
C:\Carbonite\Main_interface_file.py in <module>()
    136 
    137 root = Tk()
--> 138 app = App(root)
    139 root.mainloop()
    140 

C:\Carbonite\Main_interface_file.py in __init__(self, master)
     72         self.img = ImageTk.PhotoImage(self.image)
     73         #self.label = Label(image = self.img)
---> 74         self.label = Label(frame,image = self.img)
     75         self.label.image = self.img
     76         self.label.grid(row=3, column = 0)

C:\Users\U10596\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\lib\lib-tk\Tkinter.pyc in __init__(self, master, cnf, **kw)
   2585 
   2586         """
-> 2587         Widget.__init__(self, master, 'label', cnf, kw)
   2588 
   2589 class Listbox(Widget, XView, YView):

C:\Users\U10596\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\lib\lib-tk\Tkinter.pyc in __init__(self, master, widgetName, cnf, kw, extra)
   2084                 del cnf[k]
   2085         self.tk.call(
-> 2086             (widgetName, self._w) + extra + self._options(cnf))
   2087         for k, v in classes:
   2088             k.configure(self, v)

TclError: image "pyimage8" doesn't exist 
import matplotlib.pyplot as plt
from Tkinter import *
from PIL import ImageTk, Image
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
from images_to_list import images_to_list
from tkFileDialog import askopenfilename, askdirectory
#Creating a class that creates the UI
class App:

    def __init__(self, master):

        self.event_num = 1
        # Create a container
        frame = Frame(master)

        # Create 2 buttons (changes between leakage events
        self.button_left = Button(frame,text="< Previous Event",
                                        command=self.decrease)
        self.button_left.grid(row=1,column=0)
        self.button_right = Button(frame,text="Next Event >",
                                        command=self.increase)
        self.button_right.grid(row=1,column=3)

       #Creating text for the UI indicating the number of leakage events
        w = Label(frame, text="/  %s  " % len(tft))
        w.grid(row=1,column=2)

        #Display the number of the current event in the series
        self.m = Canvas(frame,width=50,height=25)
        self.text_id = self.m.create_text(25,12.5, text="%s" % (self.event_num+1))
        self.m.grid(row=1,column=1)

        #Creating the plot of voltage data
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        self.fig.autofmt_xdate()
        import matplotlib.dates as mdates
        self.ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
        self.line, = self.ax.plot(tft[self.event_num],tf1[self.event_num],'.')
        self.line2, = self.ax.plot(tft[self.event_num],tf2[self.event_num],'.')
        self.ax.set_ylim([0,3.5])


        self.path = "C:\Carbonite\EL_36604.02_231694\EL_36604.02_231694_2015-06-15 10.39.57.jpeg"
        self.image = Image.open(self.path)
        self.img = ImageTk.PhotoImage(self.image)
        #self.label = Label(image = self.img)
        self.label = Label(frame,image = self.img)
        self.label.image = self.img
        self.label.grid(row=3, column = 0)


        self.canvas = FigureCanvasTkAgg(self.fig,master=master)
        self.canvas.show()
        self.canvas.get_tk_widget().grid(row=1,column=0)
        frame.grid(row=0,column=0)

        #Creating a textbox to jump to event number
        self.textbox = Entry(frame,width=5)
        button1 = Button(frame, text='Go', command=self.letsgo) #Linking "Go" button with letsgo function to jump to event number
        self.textbox.grid(row=2,column=1)
        button1.grid(row=2,column=2)

    #function letsgo allows the user to jump to any event in the series    
    def letsgo(self):
        txt = self.textbox.get()
        try:
            self.event_num = int(txt)
        except ValueError:
            print "Opps! The number you enter needs to be an integer!"
        self.line.set_xdata(tft[self.event_num])
        self.line.set_ydata(tf1[self.event_num])
        self.line2.set_xdata(tft[self.event_num])
        self.line2.set_ydata(tf2[self.event_num])
        self.ax.set_xlim([min(tft[self.event_num]),max(tft[self.event_num])])
        self.canvas.draw()
        self.m.itemconfig(self.text_id, text="%s" % (self.event_num+1))

     #function decrease allows the user to use the decrease button                
    def decrease(self):
        if self.event_num == 0: #if statement accounts for if the user tries to see the event previous to the first one
            self.event_num = len(tft)-1
        else:
            self.event_num -= 1
        self.line.set_xdata(tft[self.event_num])
        self.line.set_ydata(tf1[self.event_num])
        self.line2.set_xdata(tft[self.event_num])
        self.line2.set_ydata(tf2[self.event_num])
        self.ax.set_xlim([min(tft[self.event_num]),max(tft[self.event_num])])
        self.canvas.draw()
        self.m.itemconfig(self.text_id, text="%s" % (self.event_num+1))

    #function increase allows the user to use the increase button            
    def increase(self):
        if self.event_num == len(tft)-1: #if statement accounts for if the user tries to see the event after the last one.
            self.event_num = 0
        else:
            self.event_num += 1        
        self.line.set_xdata(tft[self.event_num])
        self.line.set_ydata(tf1[self.event_num])
        self.line2.set_xdata(tft[self.event_num])
        self.line2.set_ydata(tf2[self.event_num])
        self.ax.set_xlim([min(tft[self.event_num]),max(tft[self.event_num])])
        self.canvas.draw()
        self.m.itemconfig(self.text_id, text="%s" % (self.event_num+1))





root = Tk()
app = App(root)
root.mainloop()
frame.grid()