【问题标题】:How to draw images in tkinter window如何在 tkinter 窗口中绘制图像
【发布时间】:2012-08-28 07:46:45
【问题描述】:

如何在 tkinter 窗口中绘制图像(我使用的是 python 3.3)?我正在寻找可以在 tkinter 窗口的给定位置绘制图像的语句。

是的……

任何答案将不胜感激。这是我想在其中使用代码的程序的源代码(如果可以调用的话),以备不时之需。

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

我很清楚间距有点乱,但这是在复制时发生的事情

【问题讨论】:

    标签: python image user-interface tkinter draw


    【解决方案1】:

    您需要使用Canvas 小部件将您的图像放置在指定的 (x,y) 位置。

    在 Python 3 中,您可以这样做:

    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* 版本,您只能阅读 GIFPGMPPM 类型的图像 - 如果您需要其他文件类型,请查看this answer

    Canvas 小部件非常强大,可让您定位图像,通过"canvas.update" 调用显示其上的内容,并通过"canvas.delete(item_id)" 调用删除项目显示器。检查其documentation

    虽然 Tkinter 应该足以满足您的简单游戏,但请考虑查看 Pygame,以获得更好的多媒体支持,或者可能是 Pyglet,或者甚至称为 Kivy 的更高级别的多媒体框架。

    *(更新):截至 2015 年,有 Pillow - 一个替代旧 PIL 项目的分支,它恢复了项目的正常开发,包括对 Python 的支持3.x

    【讨论】:

      【解决方案2】:

      该示例在画布上显示图像。

      from PIL import Image, ImageTk
      

      从 PIL (Python Imaging Library) 模块中,我们导入 Image 和 ImageTk 模块。

      self.img = Image.open("tatras.jpg") //your image name :)
      self.tatras = ImageTk.PhotoImage(self.img)
      

      Tkinter 内部不支持 JPG 图片。作为一种解决方法,我们 使用 Image 和 ImageTk 模块。

      canvas = Canvas(self, width=self.img.size[0]+20,
          height=self.img.size[1]+20)
      

      我们创建 Canvas 小部件。它考虑了图像的大小。它比实际图像尺寸宽 20px,高 20px。

      canvas.create_image(10, 10, anchor=NW, image=self.tatras)
      

      参考见:https://tutorialspoint.com/python/tk_canvas.htm

      【讨论】:

        【解决方案3】:

        这在很大程度上取决于文件格式。 Tkinter 有一个PhotoImage 类,如果您的图像是.gif,它可以很容易地在Labels 中使用。您还可以轻松地将它们添加到画布小部件中。否则,您可能需要使用 PIL 将图像转换为 PhotoImage

        【讨论】: