944,167 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 18985
  • Python RSS
May 26th, 2007
0

Putting an image into a Tkinter thingy

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marigold23 is offline Offline
2 posts
since Apr 2007
May 27th, 2007
0

Re: Putting an image into a Tkinter thingy

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.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
fredzik is offline Offline
55 posts
since Feb 2007
May 27th, 2007
0

Re: Putting an image into a Tkinter thingy

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.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
fredzik is offline Offline
55 posts
since Feb 2007
May 27th, 2007
0

Re: Putting an image into a Tkinter thingy

Here is the use of the Python Image Library, works well with Tkinter, to get image formats other than .gif ...
python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
May 28th, 2007
0

Re: Putting an image into a Tkinter thingy

Thankyou Fredzik and Vegaseat for your fast replies Im very grateful thankyou!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marigold23 is offline Offline
2 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: new Python knowledge web
Next Thread in Python Forum Timeline: wxCanvas-Get current coordinates





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC