【问题标题】:Insert a .jpg in a canvas with tkinter and Python 3.2使用 tkinter 和 Python 3.2 在画布中插入 .jpg
【发布时间】:2013-05-14 09:23:32
【问题描述】:

所以我想将 .jpg 放在画布中,我在互联网上找到的只是使用 PIL,但我使用的是 Python 3.2,所以 PIL 不起作用。 如何使用 Python 3.2 在画布中插入 .jpg?

【问题讨论】:

    标签: python python-3.x tkinter jpeg


    【解决方案1】:

    只是为了让现在看到这个的其他人免于四处寻找零碎的东西(就像我刚刚做的那样)

    正如 Martijn Pieters 所说,使用 Pillow 而不是 PIL,但代码看起来相同

    from tkinter import Tk, Canvas
    from PIL import ImageTk, Image
    
    root = Tk()
    
    #Create a canvas
    canvas = Canvas(root, width=400, height=300)
    canvas.pack()
    
    # Load the image file
    im = Image.open('test_image.jpg')
    # Put the image into a canvas compatible class, and stick in an
    # arbitrary variable to the garbage collector doesn't destroy it
    canvas.image = ImageTk.PhotoImage(im)
    # Add the image to the canvas, and set the anchor to the top left / north west corner
    canvas.create_image(0, 0, image=canvas.image, anchor='nw')
    
    root.mainloop()
    

    【讨论】:

      【解决方案2】:

      PIL 确实在 Python 3.2 上工作;安装 Pillow,友好的 PIL 分支。

      Pillow 2.0.0 增加了对 Python 3 的支持,并包含许多来自 Internet 的错误修复。

      【讨论】:

      • 我找到并安装了 Pillow,但不知道你如何使用它
      • @MathieuRobert:只是像 PIL 一样使用它。相同的导入,相同的代码。
      【解决方案3】:

      您只需要在画布中创建图像。确保您的图像与您的代码位于同一文件夹中。

          image = PhotoImage (file="image.jpg")
          yourcanvas.canvas.create_image (0, 0, anchor=NW, image=image, tags="bg_img")
      

      应该这样做。这也会将画布扩展到图像的大小,只是为了让您知道。祝你的项目好运!

      【讨论】:

        猜你喜欢
        • 2020-01-24
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 2020-05-15
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 2013-01-03
        相关资源
        最近更新 更多