3 Replies - 222 Views - Last Post: 25 August 2013 - 06:43 AM Rate Topic: -----

#1 higharc91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-June 13

Tkinter image question

Posted 24 August 2013 - 06:30 AM

I am following a tutorital on how to add a photo into Tkinter, this photo I am trying to insert is going to be my programs logo but I cannot for the life of me get it to load..
All that shows up is a little black screen that is the size of the photo but no image is loaded. What am I doing wrong here? The photo is in the same directory as the python script.
I have also tried a JPEG photo but no luck..

from Tkinter import *
import Image, ImageTk


class App():

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        image = Image.open("pinger2.gif")
        photo = ImageTk.PhotoImage(image)
        self.logo = Label(root, image = photo)
        self.logo.photo = image #this re references the image
        self.logo.pack()

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



Is This A Good Question/Topic? 0
  • +

Replies To: Tkinter image question

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3078
  • View blog
  • Posts: 10,284
  • Joined: 12-December 12

Re: Tkinter image question

Posted 24 August 2013 - 06:50 AM

I assume you should be importing from the Python Imaging Library:

from PIL import ImageTk, Image

but if this is the case then I'm surprised the code runs.

This post has been edited by andrewsw: 24 August 2013 - 06:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • Joined: 21-November 12

Re: Tkinter image question

Posted 24 August 2013 - 10:26 AM

You did not quite get the "keep a reference to the image" correct-you want to keep a reference to the "image=" object. You can use the following modified code or simply self.photo will create an instance object that remains as long as the class is alive, i.e. even after you exit the class and call mainloop().
from Tkinter import *
import Image, ImageTk
 
 
class App():
 
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        image = Image.open("pinger2.gif")
        photo = ImageTk.PhotoImage(image)
        self.logo = Label(root, image = photo)
##        self.logo.photo = image #this re references the image
        self.logo.photo = photo #this re references the image

        self.logo.pack()
 
root = Tk()
app = App(root)
root.mainloop() 

This post has been edited by woooee: 24 August 2013 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 higharc91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-June 13

Re: Tkinter image question

Posted 25 August 2013 - 06:43 AM

Quote

You did not quite get the "keep a reference to the image" correct-you want to keep a reference to the "image=" object. You can use the following modified code or simply self.photo will create an instance object that remains as long as the class is alive, i.e. even after you exit the class and call mainloop().


Thanks man! Tha was it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1