Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw images in tkinter window

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

like image 593
pipsqueaker117 Avatar asked Sep 03 '12 14:09

pipsqueaker117


1 Answers

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

like image 53
jsbueno Avatar answered Oct 07 '22 12:10

jsbueno