如何在tkinter窗口中绘制图像 [英] How to draw images in tkinter window
问题描述
如何在tkinter窗口中绘制图像(我使用的是python 3.3)?我正在寻找一个声明,它会在tkinter窗口的给定位置绘制图像。
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.
是啊...
任何答案都将不胜感激。这里是程序的源代码(如果它可以被称为),我想使用代码,以防你需要它。
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
推荐答案
您需要使用 Canvas
小部件将图像放入特定的ed(x,y)position。
You will need to use a Canvas
widget to put your images in specified (x,y) positions.
在Python 3中,你可以这样做:
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)
请注意,由于Python 3没有正式的 PIL
* 版本,你是仅限于读取 GIF
类型的图像, PGM
或 PPM
- 如果您需要其他文件类型,请查看此答案。
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.
Canvas小部件非常强大,允许您定位图像,通过canvas.update显示其中的内容
调用,并使用canvas.delete(item_id)
调用删除项目显示器。查看其文档。
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.
虽然Tkinter应该足够您的简单游戏,但请考虑查看 Pygame
,以获得更好的多媒体支持,或者 Pyglet ,甚至是更高级别的多媒体框架,名为 Kivy 。
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.
* (更新):截至2015年,有一个Pillow - 一个替代旧PIL项目的分支,它恢复了项目的正确开发,包括支持对于Python 3.x
*(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
这篇关于如何在tkinter窗口中绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!