954,202 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Putting an image into a Tkinter thingy

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

marigold23
Newbie Poster
2 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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.

fredzik
Junior Poster in Training
55 posts since Feb 2007
Reputation Points: 10
Solved Threads: 5
 

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:icon_smile:.

fredzik.

fredzik
Junior Poster in Training
55 posts since Feb 2007
Reputation Points: 10
Solved Threads: 5
 

Here is the use of the Python Image Library, works well with Tkinter, to get image formats other than .gif ...

# use a Tkinter label as a panel/frame with a background image
# note that Tkinter only reads gif and ppm images
# use the Python Image Library (PIL) for other image formats
# free from  <a href="http://www.pythonware.com/products/pil/index.htm">http://www.pythonware.com/products/pil/index.htm</a> 
# give Tkinter a namespace to avoid conflicts with PIL
# (they both have a class named Image)
 
import Tkinter as tk
from PIL import Image, ImageTk
 
root = tk.Tk()
root.title('background image')
 
# pick an image file you have .bmp  .jpg  .gif.  .png
# load the file and covert it to a Tkinter image object
imageFile = "Flowers.jpg"
image1 = ImageTk.PhotoImage(Image.open(imageFile))
 
# get the image size
w = image1.width()
h = image1.height()
 
# position coordinates of root 'upper left corner'
x = 0
y = 0
 
# make the root window the size of the image
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
 
# root has no image argument, so use a label as a panel
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
 
# put a button on the image panel to test it
button2 = tk.Button(panel1, text='button2')
button2.pack(side='top')
 
# save the panel's image from 'garbage collection'
panel1.image = image1
 
# start the event loop
root.mainloop()

Please use the[code=python] and [/code] tag pair to enclose your python code.

vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

Thankyou Fredzik and Vegaseat for your fast replies Im very grateful thankyou!! :)

marigold23
Newbie Poster
2 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You