470,572 Members | 1,916 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,572 developers. It's quick & easy.

Tkinter, image not appearing in function but without function

why is the first program not working? when i click the screen the map
is not appearing.

the second program works.

from Tkinter import *

master = Tk()

w = Canvas(master, width=700, height=600)
w.pack(expand = YES, fill = BOTH)

def mapper():
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\world-map.gif')
w.create_image(10, 10, image = mapq, anchor = NW)

def key(event):
print "pressed", repr(event.char)

def callback(event):
w.focus_set()
print "clicked at", event.x, event.y
mapper()
print 'yo'
square = w.create_oval(event.x-5,event.y-5,event.x+5,event.y+5,
fill="black")

w.bind("<Key>", key)
w.bind("<Button-1>", callback)
w.pack()
mainloop()


from Tkinter import *

master = Tk()
w = Canvas(master, width=700, height=600)
w.pack(expand = YES, fill = BOTH)

q=raw_input("Choose: i=India, s=sweden, else World: ")
if q=="i":
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\map-india.bmp')
elif q=='s':
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\provinces-of-
sweden.gif')
else:
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\world-map.gif')

print mapq.height(), mapq.width()
w.create_image(10, 10, image = mapq, anchor = NW)

def key(event):
print "pressed", repr(event.char)

def callback(event):
w.focus_set()
print "clicked at", event.x, event.y
square = w.create_oval(event.x-5,event.y-5,event.x+5,event.y+5,
fill="black")

"""hur hitta om inom ratt->kolla color of """

w.bind("<Key>", key)
w.bind("<Button-1>", callback)
w.pack()

mainloop()
Jun 27 '08 #1
2 1326
On Sun, 13 Apr 2008 08:57:29 -0700, skanemupp wrote:
why is the first program not working? when i click the screen the map
is not appearing.

the second program works.

from Tkinter import *

master = Tk()

w = Canvas(master, width=700, height=600)
w.pack(expand = YES, fill = BOTH)

def mapper():
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\world-map.gif')
w.create_image(10, 10, image = mapq, anchor = NW)

def key(event):
print "pressed", repr(event.char)

def callback(event):
w.focus_set()
print "clicked at", event.x, event.y
mapper()
print 'yo'
square = w.create_oval(event.x-5,event.y-5,event.x+5,event.y+5,
fill="black")

w.bind("<Key>", key)
w.bind("<Button-1>", callback)
w.pack()
mainloop()
Python doesn't know that the Tk side still needs the image and frees the
memory when `mapper()` is done and `mapq` the only name to the `PhotoImage`
instance goes out of scope.

Ciao,
Marc 'BlackJack' Rintsch
Jun 27 '08 #2
On Apr 13, 11:12*am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Sun, 13 Apr 2008 08:57:29 -0700, skanemupp wrote:
why is the first program not working? when i click the screen the map
is not appearing.
the second program works.
from Tkinter import *
master = Tk()
w = Canvas(master, width=700, height=600)
w.pack(expand = YES, fill = BOTH)
def mapper():
* * mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\world-map..gif')
* * w.create_image(10, 10, image = mapq, anchor = NW)
def key(event):
* * print "pressed", repr(event.char)
def callback(event):
* * w.focus_set()
* * print "clicked at", event.x, event.y
* * mapper()
* * print 'yo'
* * square = w.create_oval(event.x-5,event.y-5,event.x+5,event.y+5,
fill="black")
w.bind("<Key>", key)
w.bind("<Button-1>", callback)
w.pack()
mainloop()

Python doesn't know that the Tk side still needs the image and frees the
memory when `mapper()` is done and `mapq` the only name to the `PhotoImage`
instance goes out of scope.

Ciao,
* * * * Marc 'BlackJack' Rintsch

...so you should do something like this(untested):
def mapper(height, width, widget, img, position):
widget.create_image(height, width, image=img, anchor=position)

w = Canvas(master, width=700, height=600)
w.pack(expand = YES, fill = BOTH)

#Create a permanent reference to the image, i.e. the variable is not
#created inside a function:
my_img = PhotoImage(file = 'C:\Users\saftarn\Desktop\world-map.gif')

mapper(10, 10, w, my_img, NW)
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.