Tkinter

J

Jay

Cold somebody please enlighten me at why code 1 works perfectly and yet
code to will not display the gif file.

code 1
-----------START---------

from Tkinter import *

root = Tk()

MainFrame = Canvas(root)
MainFrame.pack()

BackgroundFile = PhotoImage(file="Background.GIF")
Background = MainFrame.create_image(0, 0, image=BackgroundFile)

root.mainloop()

-----------END---------

code 2
-----------START---------

from Tkinter import *

class App:
def __init__(self, root):
self.MainFrame = Canvas(root)
self.MainFrame.pack(fill=BOTH, expand=1)

BackgroundFile = PhotoImage(file="Background.GIF")
Tank1 = self.MainFrame.create_image(0, 0, image=BackgroundFile)

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

-----------END---------
Thanks

Jay
 
R

Robert Hicks

Maybe because Tkinter comes *with* Python? It is sometimes easier to
understand the way things work in Tkinter vs wxPython as well. Though I
will add that Manning has just published "wxPython in Action" so that
should help newbies out as well.

Robert
 
F

Fredrik Lundh

Jay said:
Cold somebody please enlighten me at why code 1 works perfectly and yet
code to will not display the gif file.
code 2
-----------START---------

from Tkinter import *

class App:
def __init__(self, root):
self.MainFrame = Canvas(root)
self.MainFrame.pack(fill=BOTH, expand=1)

BackgroundFile = PhotoImage(file="Background.GIF")
Tank1 = self.MainFrame.create_image(0, 0, image=BackgroundFile)

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

-----------END---------
Thanks

you need to keep a reference to the photo object on the Python side
of things, to keep it from being garbage collected.

see the note at the bottom of this page:

http://www.effbot.org/tkinterbook/photoimage.htm

</F>
 
J

Jay

Now I just get this error message.

AttributeError: 'int' object has no attribute 'image'

But the picture appears so I am almost their.

---START---

from Tkinter import *

class App:
def __init__(self, root):
self.MainFrame = Canvas(root)
self.MainFrame.pack(fill=BOTH, expand=1)

BackgroundFile = PhotoImage(file="Background.GIF")
Background = self.MainFrame.create_image(0, 0,
image=BackgroundFile)
Background.image = BackgroundFile # keep a reference!


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

Fredrik Lundh

Jay said:
Now I just get this error message.

AttributeError: 'int' object has no attribute 'image'

But the picture appears so I am almost their.

---START---

from Tkinter import *

class App:
def __init__(self, root):
self.MainFrame = Canvas(root)
self.MainFrame.pack(fill=BOTH, expand=1)

BackgroundFile = PhotoImage(file="Background.GIF")
Background = self.MainFrame.create_image(0, 0,
image=BackgroundFile)
Background.image = BackgroundFile # keep a reference!

the example on that page attaches the image to a widget instance,
not a canvas object handle (which is an integer; unlike user-defined
classes, integers don't allow you to attach arbitrary attributes to
them).

if you just want to display a single image, just attach it to self:

BackgroundFile = PhotoImage(file="Background.GIF")
Background = self.MainFrame.create_image(0, 0, image=BackgroundFile)
self.image = BackgroundFile # keep a reference!

if you want to display multiple images on the canvas, use a dictionary
or a list to hold active image references.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top