| | |
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:
Solved Threads: 0
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
# 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
•
•
Join Date: Feb 2007
Posts: 55
Reputation:
Solved Threads: 4
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.
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.
•
•
Join Date: Feb 2007
Posts: 55
Reputation:
Solved Threads: 4
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.
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.
Here is the use of the Python Image Library, works well with Tkinter, to get image formats other than .gif ...
Please use the [code=python] and [/code] tag pair to enclose your python code.
python Syntax (Toggle Plain Text)
# 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()
Last edited by vegaseat; May 27th, 2007 at 12:53 pm.
May 'the Google' be with you!
![]() |
Similar Threads
- Tkinter Image Problem (Python)
- Image position ??? (HTML and CSS)
- putting an image (Java)
Other Threads in the Python Forum
- Previous Thread: new Python knowledge web
- Next Thread: wxCanvas-Get current coordinates
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor class clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically events examples external file float format frange function google gui hints homework i/o iframe import info input java line linux list lists loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop windows word wxpython






