Image not displayed on label

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nazish zehra
    New Member
    • Dec 2011
    • 31

    Image not displayed on label

    I am making a label containing a gif image.Image is not displayed and program gives the following error :
    Code:
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
        return self.func(*args)
      File "D:\7th sem\abiabi\pythoproj2\toplevelchanges.py", line 114, in about
        label = Label(imageff,image=photo)
      File "C:\Python26\lib\lib-tk\Tkinter.py", line 2464, in __init__
        Widget.__init__(self, master, 'label', cnf, kw)
      File "C:\Python26\lib\lib-tk\Tkinter.py", line 1930, in __init__
        (widgetName, self._w) + extra + self._options(cnf))
    TclError: image "pyimage3" doesn't exist
    My Code for image label is:
    Code:
    imageff=Frame(root1)
    photo = PhotoImage(file="toolbar-search.gif")
    label = Label(imageff,image=photo)
    imageff["pady"]=10
    label.pack()
    imageff.pack()
    ......#other code of program
    root1.pack()#at the end
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    TclError: image "pyimage3" doesn't exist
    Code:
    photo = PhotoImage(file="/path/to/file/toolbar-search.gif")
    To start debugging, try using the full file path and post back with any additional errors.

    Comment

    • nazish zehra
      New Member
      • Dec 2011
      • 31

      #3
      I have tried the full path but it is giving the same Error.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Tkinter garbage collects image objects that are stored in local variables after a function returns even if it is being displayed. To avoid this, keep an extra reference by creating an attribute. Example:
        Code:
        import Tkinter
        class Application(Tkinter.Frame):
            def __init__(self, master=None):
                Tkinter.Frame.__init__(self, master)
                self.master = master
                self.grid()
                self.image = self.createWidgets()
        
            def createWidgets(self):
                self.quitButton = Tkinter.Button (self, text='Quit',
                                                  command=self.quit)
                self.quitButton.grid()
                
                img = Tkinter.PhotoImage(file="image.gif")
                w = Tkinter.Label(self,image=img)
                w.grid()
                return img
                
        root = Tkinter.Tk()
        app = Application(root)
        app.master.title("Sample application")
        root.mainloop()

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          That is true but it would then just create an empty/blank label. It would not give an "image doesn't exist error" which can mean only one thing, that the program can not find the image. So something here doesn't make sense; either the posted error message was different from the actual message, or the path used to find the image was incorrect.

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            The original code does display an image on my computer so it almost has to be a path problem.
            Code:
            try:
                import Tkinter as tk     ## Python 2.x
            except ImportError:
                import tkinter as tk     ## Python 3.x
            
            root1 = tk.Tk()
            imageff=tk.Frame(root1)
            fname='/home/david/python/Tkinter/Grayson/Examples/Chapter04/img52.gif'
            photo = tk.PhotoImage(file=fname)
            label = tk.Label(imageff,image=photo)
            label.photo=photo     ## keep a copy
            imageff["pady"]=10
            label.pack()
            imageff.pack()
            root1.mainloop()

            Comment

            • nazish zehra
              New Member
              • Dec 2011
              • 31

              #7
              It is not path problem because image is there it is something else and "pyimag3" does not exist is not path error.I am displaying image label by callling a function and if i remove the image then everything else is displayed but with label it is not.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                nazish zehra,

                Is your problem solved?

                Comment

                • nazish zehra
                  New Member
                  • Dec 2011
                  • 31

                  #9
                  No it is not solved yet.

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    nazish zehra - Would you post your code with the problem (enough of your code so we can test it) and any error message you received?

                    Comment

                    • nazish zehra
                      New Member
                      • Dec 2011
                      • 31

                      #11
                      following is the function which i am calling
                      Code:
                      def about():
                              root1 = Tk()
                              root1.title("ABOUT")
                              root1["padx"] = 25
                              root1["pady"] = 25
                      
                      
                              TopLabel = Label(root1)
                      
                              TopLabel["text"] ="         STRING SEARCHER            "
                              TopLabel["font"]="Helvetica",12
                              TopLabel["relief"]= RIDGE
                              TopLabel.pack()
                      
                      
                              image_name = "toolbar-search.gif"
                              imageff =PhotoImage(file=image_name)
                              imageff=Frame(root1)
                              label = Label(imageff,image=photo)
                              label.image = photo 
                              imageff["pady"]=10
                              label.pack()
                              imageff.pack()
                              root1.mainloop()
                      and next i am calling it from the same program.Calling the function by making a menubar.Code is:
                      Code:
                      root = Tk()
                      menubar = Menu(root)
                      helpmenu = Menu(menubar, tearoff=0)
                      helpmenu.add_command(label="About...", command=about)
                      root.config(menu=menubar)
                      
                      root.mainloop()
                      and error is same that "pyimage3" doesnot exist

                      Comment

                      • dwblas
                        Recognized Expert Contributor
                        • May 2008
                        • 626

                        #12
                        Code:
                                label = Label(imageff,image=photo)
                        The variable "photo" has not been declared in the program. Also, imageff is used twice
                        Code:
                                imageff =PhotoImage(file=image_name)
                                imageff=Frame(root1)
                        Take another look at the working code posted above.

                        Comment

                        Working...