Reputation: 2366
How do I draw an image in a tkinter window (I'm using python 3.3)? I'm looking for a statement which would draw an image at a given position on a tkinter window.
yeah...
Any answers would be appreciated. And here's the source code of the program (if it can be called that) that I want to use the code in, in case you need it.
from tkinter import *
class craftClass():
def __init__(self, x = 80, y = 80, xmotion = 0, ymotion = 0, health = 20):
self.xPos, self.yPos = x, y
self.dx, self.dy = xmotion, ymotion
def moveCraft(self):
self.xPos += self.dx
self.yPos += self.dy
class missileClass():
def __init__(self, x = 0 , y = 0):
self.xPos, self.yPos = x, y
class alienClass():
def __init__(self, x, y):
self.xPos, self.yPos = x, y
def moveForCraft(self, craftX, craftY):
if self.xPos < craftX:
self.xPos += 2
elif self.xPos > craftX:
self.xPos -= 2
else:
pass
if self.yPos < craftY:
self.yPos += 2
elif self.yPos > craftY:
self.yPos -= 2
else:
pass
craft = craftClass()
missileArray = []
alienArray = []
def keypress(event):
if event.keysym == 'Escape':
root.destroy()
x = event.char
if x == "w":
craft.dy = 1
elif x == "s":
craft.dy = -1
elif x == "a":
craft.dx = -1
elif x == "d":
craft.dx = 1
else:
print(x)
root = Tk()
print(craft.dx)
while True:
try:
root.bind_all('<Key>', keypress)
craft.moveCraft()
root.update()
except TclError:
print("exited. tcl error thrown. llop broken")
break
I'm well aware that the spacing is sorta messed up, but that's something that happened while copying
Upvotes: 11
Views: 31485
Reputation:
from PIL import Image, ImageTk
From the PIL (Python Imaging Library) module, we import the Image and ImageTk modules.
self.img = Image.open("tatras.jpg") //your image name :)
self.tatras = ImageTk.PhotoImage(self.img)
Tkinter does not support JPG images internally. As a workaround, we use the Image and ImageTk modules.
canvas = Canvas(self, width=self.img.size[0]+20,
height=self.img.size[1]+20)
We create the Canvas widget. It takes the size of the image into account. It is 20px wider and 20px higher than the actual image size.
canvas.create_image(10, 10, anchor=NW, image=self.tatras)
Reference see: https://tutorialspoint.com/python/tk_canvas.htm
Upvotes: 4
Reputation: 110301
You will need to use a Canvas
widget to put your images in specified (x,y) positions.
In Python 3, you can do like this:
import tkinter
tk = tkinter.Tk()
can = tkinter.Canvas(tk)
can.pack()
img = tkinter.PhotoImg("<path/to/image_file>.gif")
can.create_image((x_coordinate, y_coordinate), img)
Please note that due to Python 3 not having an official PIL
* release, you are limited to read images of type GIF
, PGM
or PPM
- if you need other file types, check this answer.
The Canvas widget is quite powerfull, and allows you to position your images, shows what is on it through an "canvas.update"
call, and remove an item displayer with a "canvas.delete(item_id)"
call. Check its documentation.
While Tkinter should be enough for your simple game, consider taking a look at Pygame
, for a better multimedia support, or maybe Pyglet, or even higher level multimedia framework called Kivy.
*(update): As of 2015, there is Pillow - a fork that is a drop in replacement of the old PIL project, and which resumed proper development of the project, including support for Python 3.x
Upvotes: 10
Reputation: 309929
This depends a lot on the file format. Tkinter has a PhotoImage class which can be used in Labels
quite easily if your image is a .gif
. You can also add them to canvas widgets reasonably easily. Otherwise, you might want to use PIL to convert an image to a PhotoImage
.
Upvotes: 1