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
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
如果你想用线条,圆圈等绘制东西,那么画布小部件就是使用的东西。
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.