Putting an image into a Tkinter thingy

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2007
Posts: 2
Reputation: marigold23 is an unknown quantity at this point 
Solved Threads: 0
marigold23 marigold23 is offline Offline
Newbie Poster

Putting an image into a Tkinter thingy

 
0
  #1
May 26th, 2007
Hey there everyone. well i have been working on python for about 2 months now and i just cant seem to get my background image to work. I have created the GUI with Tkinter and put a label in it with my name etc, but the idea was to have a picture of myself in the GUI. This is what i have so far, everything works (GUI and label) but just not the background image .

# A GUI displaying a background picture

from Tkinter import *

# create the window
root = Tk()
root.title("GUI program")
root.geometry("640x510")

# create a frame in the window to hold widgets
app = Frame(root)
app.grid()

# create a label in the frame
lbl = Label(app, text = "Hi, my name is Greer!")
lbl.grid()

# kick off the windows loop
root.mainloop()

# load background image


def main():
room_image = load_image("c:\Python23" "room_image.jpg")
background = room_image
the_room = Room(image = room_image,
screen_width = 100,
screen_height = 500,
fps = 50)
add(the_room)

main()

can somone please help me i just cant seem to figure out what exaclty im doing wrong.

thankyou
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 55
Reputation: fredzik is an unknown quantity at this point 
Solved Threads: 4
fredzik fredzik is offline Offline
Junior Poster in Training

Re: Putting an image into a Tkinter thingy

 
0
  #2
May 27th, 2007
Hi,

The problem seems to be the following... "room_image.jpg"...from what I know about Tkinter it only accepts the .gif type of images. There are one or two free image converters on the internet that convert a whole plethora of images such as .jpg etc to the .gif type of image format.

If you want a free image converter try here http://www.homeplansoftware.com/imgconv.htm

I've used it and it works just fine.

fredzik.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 55
Reputation: fredzik is an unknown quantity at this point 
Solved Threads: 4
fredzik fredzik is offline Offline
Junior Poster in Training

Re: Putting an image into a Tkinter thingy

 
0
  #3
May 27th, 2007
P.S.

As others may want to test your code and send you help, please email your code again and in the "Reply to Thread" Message box do the following:

At the end of your code, after you've pasted it in, type this [/code] including brackets.

Before you paste in your code, type this [code=Python] including brackets.

The reason I've done this upside down was because the Message box kept on thinking I was trying to put some code in.

Anyway, good luck with your replies.

fredzik.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,046
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Putting an image into a Tkinter thingy

 
0
  #4
May 27th, 2007
Here is the use of the Python Image Library, works well with Tkinter, to get image formats other than .gif ...
  1. # use a Tkinter label as a panel/frame with a background image
  2. # note that Tkinter only reads gif and ppm images
  3. # use the Python Image Library (PIL) for other image formats
  4. # free from [url]http://www.pythonware.com/products/pil/index.htm[/url]
  5. # give Tkinter a namespace to avoid conflicts with PIL
  6. # (they both have a class named Image)
  7.  
  8. import Tkinter as tk
  9. from PIL import Image, ImageTk
  10.  
  11. root = tk.Tk()
  12. root.title('background image')
  13.  
  14. # pick an image file you have .bmp .jpg .gif. .png
  15. # load the file and covert it to a Tkinter image object
  16. imageFile = "Flowers.jpg"
  17. image1 = ImageTk.PhotoImage(Image.open(imageFile))
  18.  
  19. # get the image size
  20. w = image1.width()
  21. h = image1.height()
  22.  
  23. # position coordinates of root 'upper left corner'
  24. x = 0
  25. y = 0
  26.  
  27. # make the root window the size of the image
  28. root.geometry("%dx%d+%d+%d" % (w, h, x, y))
  29.  
  30. # root has no image argument, so use a label as a panel
  31. panel1 = tk.Label(root, image=image1)
  32. panel1.pack(side='top', fill='both', expand='yes')
  33.  
  34. # put a button on the image panel to test it
  35. button2 = tk.Button(panel1, text='button2')
  36. button2.pack(side='top')
  37.  
  38. # save the panel's image from 'garbage collection'
  39. panel1.image = image1
  40.  
  41. # start the event loop
  42. root.mainloop()
Please use the [code=python] and [/code] tag pair to enclose your python code.
Last edited by vegaseat; May 27th, 2007 at 12:53 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 2
Reputation: marigold23 is an unknown quantity at this point 
Solved Threads: 0
marigold23 marigold23 is offline Offline
Newbie Poster

Re: Putting an image into a Tkinter thingy

 
0
  #5
May 28th, 2007
Thankyou Fredzik and Vegaseat for your fast replies Im very grateful thankyou!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC