473,566 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble displaying image with tkinter

sj
I am just learning to use Tkinter and am having problems displaying image
files. I am able to display an image using tutorials (such as
http://www.daniweb.com/code/snippet296.html) But when I try my own code all
I get is an empty widget. What is wrong with the following program?


from Tkinter import *

class Foo(Frame):

def __init__(self,m aster=None):
Frame.__init__( self,master)
self.pack()
self.createWidg ets()

def createWidgets(s elf):

self.qbutton = Button(self)
self.qbutton["text"] = "Quit"
self.qbutton["command"] = self.quit
self.qbutton.pa ck(side = "top")

idata =
PhotoImage(file ="/home/sj/documents/projects/xaed/images/cat_001.gif")

canvas = Canvas(width=30 0,height=200)
canvas.pack(sid e="top",fill=BO TH,expand=YES)
canvas.create_i mage(50,10,imag e=idata,anchor= NW)

## lab = Label(self,imag e=idata)
## lab.pack(side=T OP)
root = Tk()
app = Foo(root)
app.mainloop()
#app.destroy()
Aug 6 '06 #1
2 9412

sj wrote:
I am just learning to use Tkinter and am having problems displaying image
files. I am able to display an image using tutorials (such as
http://www.daniweb.com/code/snippet296.html) But when I try my own code all
I get is an empty widget. What is wrong with the following program?

from Tkinter import *

class Foo(Frame):

def __init__(self,m aster=None):
Frame.__init__( self,master)
self.pack()
self.createWidg ets()
def createWidgets(s elf):

self.qbutton = Button(self)
self.qbutton["text"] = "Quit"
self.qbutton["command"] = self.quit
self.qbutton.pa ck(side = "top")

idata =
PhotoImage(file ="/home/sj/documents/projects/xaed/images/cat_001.gif")

canvas = Canvas(width=30 0,height=200)
canvas.pack(sid e="top",fill=BO TH,expand=YES)
canvas.create_i mage(50,10,imag e=idata,anchor= NW)

## lab = Label(self,imag e=idata)
## lab.pack(side=T OP)
root = Tk()
app = Foo(root)
app.mainloop()
#app.destroy()
If you keep a reference of the photoImage object then it will work!
....
self.idata=
PhotoImage(file ="/home/sj/documents/projects/xaed/images/cat_001.gif")
....
canvas.create_i mage(50,10,imag e=iself.data,an chor=NW)
....
By making the PhotoImage an attribute of your object, you keep a
reference that the garbage collector will NOT collect, So you're image
will continue to exist and thus be rendered by the canvas.

JM

Aug 6 '06 #2
sj wrote in news:CW******** *********@newss vr11.news.prodi gy.com in
comp.lang.pytho n:
I am just learning to use Tkinter and am having problems displaying
image files. I am able to display an image using tutorials (such as
http://www.daniweb.com/code/snippet296.html) But when I try my own
code all I get is an empty widget. What is wrong with the following
program?
The problem is that CPython is (garbage) collecting the image.

The canvas object is using it (i.e. passing to TCL/TK) but not
keeping a reference to it. change idata to self.idata and all
should be well.
>
from Tkinter import *

class Foo(Frame):
[snip]
>
idata =
PhotoImage(file ="/home/sj/documents/projects/xaed/images/cat_001.gif")
self.idata = PhotoImage .....
canvas = Canvas(width=30 0,height=200)
Your missing a parent reference in there (self in this case) i.e.:

canvas = Canvas(self, width=300,heigh t=200)
but when I tested it it didn't seem to matter. I'd guess
however that when the layout gets more complex it will
make a difference.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Aug 6 '06 #3

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

Similar topics

2
3492
by: Rick Olson | last post by:
I'm trying to add a Tkinter interface to an existing C program with embedded python, but seem to have trouble importing Tkinter (or accessing it). I tried a simple program that would run the script "hello.py" This is hello.py: from Tkinter import * mainWindow=Tk() label=Label(mainWindow, text="Hello") label.grid(row=0,column=0)
1
6317
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF image in the central area of a simple menu-bar app as shown below in the first code sample. But, when I set up my app with a class, as shown below in...
3
8714
by: Terry Carroll | last post by:
I've got a small batch image-processing program (it adds the time a digital photo was taken to the lower right of the image), and as a feature, I wanted to show a thumbnail of each image it was being processed. I can't get the image to update, though. I trimmed it down to a basic app indicating the problem and the code is at the end of...
0
978
by: Dustan | last post by:
The title pretty much says it all. What is the easiest way in Tkinter to display an image from the internet given the URL?
6
3327
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()
5
6835
by: exhuma.twn | last post by:
As many might know, windows allows to copy an image into the clipboard by pressing the "Print Screen" button on the keyboard. Is it possible to paste such an image from the clipboard into a "Text" widget in Tkinter? Here is my first attempt with just trying to print out the image data: ----------------- def pasteImg(tgt): global...
1
5960
by: wilson | last post by:
i converted an 8bit rgb .jpg file into .pgm using adobe photoshop and a plugin from http://photoshop.pluginsworld.com/plugins/adobe/362/richard-rosenman/portable-pixmap-importer-exporter.html I want to check if this file can be properly displayed. Image opening and show() in PIL fails to do it so i tried Tkinter i created a canvas in a...
4
9126
by: skanemupp | last post by:
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\elmapovic.gif') w.create_image(10, 10, image = mapq, anchor = NW) after doing this is there any possibility of getting the characteristics of the GIF-picture(or bitmap if i use that)? it would be very helpfull if i for example could do something like canvas.getcolor(image,...
6
2183
by: Jeff | last post by:
hi asp.net 2.0 I have a image (.jpeg) stored in sql server 2005 and now I want to display it on a webpage. So I created a webpage (Image.aspx) which just writes the buffer data to the Response object. On Default.aspx I use this Image.aspx for displaying the image, but no image
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
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...
1
5485
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...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
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
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
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...

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.