473,668 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\safta rn\Desktop\worl d-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(e vent.x-5,event.y-5,event.x+5,eve nt.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("Ch oose: i=India, s=sweden, else World: ")
if q=="i":
mapq = PhotoImage(file = 'C:\Users\safta rn\Desktop\map-india.bmp')
elif q=='s':
mapq = PhotoImage(file = 'C:\Users\safta rn\Desktop\prov inces-of-
sweden.gif')
else:
mapq = PhotoImage(file = 'C:\Users\safta rn\Desktop\worl d-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(e vent.x-5,event.y-5,event.x+5,eve nt.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 1472
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\safta rn\Desktop\worl d-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(e vent.x-5,event.y-5,event.x+5,eve nt.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.net wrote:
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\safta rn\Desktop\worl d-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(e vent.x-5,event.y-5,event.x+5,eve nt.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_i mage(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\safta rn\Desktop\worl d-map.gif')

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6247
by: Martin | last post by:
Anybody know how to change the menu button that is displayed by the Tkinter Menubutton and the (derived from it) OptionMenu classes? I think it must be something from the Motif look and feel which doesn't fit with my appl which is designed primarily for running on Windows and OS X. The default button visual that appears is basically a small rectangle drawn in relief inside a larger one. I'd like to replace this with a downward pointing...
2
3732
by: Tuvas | last post by:
I've been trying to use a canvas to display different pictures on a Tkinter interface. However, it doesn't seem to update the information. Ei, I have something like this. canvas=Canvas(master,blah...) canvas.pack() def change_pic(path): global pic image=Image() #I'm using PIL to use the images, but I
3
4482
by: Tuvas | last post by:
I'm trying to display a picture on a Tkinter Canvas. It seems to work fine the first time that it is displayed. However, subsequent times running shows an error like this: TCLerror: Wrong # args: should be ".-1211472948 .-1211470996 addtag tag searchCommand ?arg arg ...? My code works like this: if (pic):
6
3336
by: Dustan | last post by:
Nobody answered last time. I guess they wanted me to give it a shot. Well, here is how I download the image (it's a class method): def download_image(self): web_download=self.opener.open(self.url) save=open("image.jpg","w") save.writelines(web_download.readlines()) save.close() web_download.close()
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.