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

Recommended Answers

All 5 Replies

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.

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.

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 [url]http://www.pythonware.com/products/pil/index.htm[/url]
# 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.

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

I tried using his code. using one of my own files however i keep getting the following error:(unicode errir) "unicodeescape" codec can't decode bytes in position 2-3:truncated\UXXXXXXXX escape.

My code is as follows:

# 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:\Users\Skynet\Pictures\Roxanne\irongear_humbucker_black_open_1000_3.gif")
    background = room_image
    the_room = Room(image = room_image,
    screen_width = 100,
    screen_height = 500,
    fps = 50)
    add(the_room)

main()

Any and all help will be greattly arpriciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.