473,712 Members | 2,875 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 9475

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
3509
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
6361
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 the second code sample, the image disappears. How can I correct this? I'm sure the answer would...
3
8777
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 this message. It should display the three listed sample images, one after another. The thing is,...
0
989
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
3340
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
6866
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 clipboardEnabled if not clipboardEnabled: return
1
5968
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 tkinter gui and tried self.myimg=PhotoImage(file="mytestpic.pgm") ...
4
9139
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, mouseclick.x,mouseclick.y) if u get the point. get the color of the image where i clicked.
6
2188
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
8699
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
9295
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
9165
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
7935
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
6617
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...
0
4457
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4713
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2099
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.