【问题标题】:Why the the image isn't displayed on the canvas? [Python3 + tkinter]为什么图像不显示在画布上? [Python3 + tkinter]
【发布时间】:2020-03-13 20:33:28
【问题描述】:

我一直在使用 tkinter 和 python 3 编写应用程序。我创建了一个画布,并试图在其上显示一个 5000x5000 像素的 gif 图像,其中画布是 2000x2000 像素,但图像没有t 在程序运行时出现。这是代码:

class drawCanvas(object):

    def __init__(self, master, width=500, height=500):
        ''' build the canvas object '''

        # class attributes
        self.master = master
        self.cWidth = width
        self.cHeight = height

        # creating the canvas object
        self.canvas = tk.Canvas(self.master, width=self.cWidth, height=self.cHeight, bg="white")
        self.canvas.grid(row=0, column=2, sticky="nwes")
        self.canvas.configure(scrollregion=(0, 0, 2000, 2000))

        # creating the scrolling
        self.scroll_x = tk.Scrollbar(self.master, orient="horizontal", command=self.canvas.xview)
        self.scroll_x.grid(row=1, column=2, sticky="ew")

        self.scroll_y = tk.Scrollbar(self.master, orient="vertical", command=self.canvas.yview)
        self.scroll_y.grid(row=0, column=3, sticky="ns")

        self.canvas.configure(yscrollcommand=self.scroll_y.set, xscrollcommand=self.scroll_x.set)

        # trying to import an image
        self.canvas.create_image(500, 500, anchor="nw", image=r'C:\Users\Luca\Desktop\electronic_simulation\src\bg\try.gif')

我想知道是否有任何解决方案,如果有请告诉我。感谢您的宝贵时间!

【问题讨论】:

标签: python-3.x canvas tkinter tkinter-canvas photoimage


【解决方案1】:

我会尝试修复您的代码,但即使在我开始之前,我可以告诉您,如果您对图像使用绝对路径,则必须将 \ 加倍,因为单个 \ 用于特殊操作或使用 / 代替。所以你的路径必须是

C:\\Users\\Luca\\Desktop\\electronic_simulation\\src\\bg\\try.gif

C:/Users/Luca/Desktop/electronic_simulation/src/bg/try.gif

编辑: 我想我解决了你的问题:

class drawCanvas(object):

    def __init__(self, master, width=500, height=500):
        ''' build the canvas object '''
        global img

        # class attributes
        self.master = master
        self.cWidth = width
        self.cHeight = height

        # creating the canvas object
        self.canvas = tk.Canvas(self.master, width=self.cWidth, height=self.cHeight, bg="white")
        self.canvas.grid(row=0, column=2, sticky="nwes")
        self.canvas.configure(scrollregion=(0, 0, 2000, 2000))

        # creating the scrolling
        self.scroll_x = tk.Scrollbar(self.master, orient="horizontal", command=self.canvas.xview)
        self.scroll_x.grid(row=1, column=2, sticky="ew")

        self.scroll_y = tk.Scrollbar(self.master, orient="vertical", command=self.canvas.yview)
        self.scroll_y.grid(row=0, column=3, sticky="ns")

        self.canvas.configure(yscrollcommand=self.scroll_y.set, xscrollcommand=self.scroll_x.set)

        # trying to import an image

        self.img = tk.PhotoImage(file = "C:\\Users\\Luca\\Desktop\\electronic_simulation\\src\\bg\\try.gif") 
        self.canvas.create_image(0, 0, anchor="nw", image=self.img)

新编辑:由于我不知道您的脚本的上下文,我创建了一个适合我的脚本,您可以复制粘贴并直接运行:

import tkinter as tk
class drawCanvas(object):

    def __init__(self, master, width=500, height=500):
        ''' build the canvas object '''
        global img

        # class attributes
        self.master = master
        self.cWidth = width
        self.cHeight = height

        # creating the canvas object
        self.canvas = tk.Canvas(self.master, width=self.cWidth, height=self.cHeight, bg="white")
        self.canvas.grid(row=0, column=2, sticky="nwes")
        self.canvas.configure(scrollregion=(0, 0, 2000, 2000))

        # creating the scrolling
        self.scroll_x = tk.Scrollbar(self.master, orient="horizontal", command=self.canvas.xview)
        self.scroll_x.grid(row=1, column=2, sticky="ew")

        self.scroll_y = tk.Scrollbar(self.master, orient="vertical", command=self.canvas.yview)
        self.scroll_y.grid(row=0, column=3, sticky="ns")

        self.canvas.configure(yscrollcommand=self.scroll_y.set, xscrollcommand=self.scroll_x.set)

        # trying to import an image

        self.img = tk.PhotoImage(file = r"C:\Users\Luca\Desktop\electronic_simulation\src\bg\try.gif")
        self.canvas.create_image(0, 0, anchor="nw", image=self.img)

    def ml(self):
        self.master.mainloop()

test = drawCanvas(tk.Tk())
test.ml()

【讨论】:

  • self.img = PhotoImage(file=...) 应该是 self.img = Image.open(...)。而你说的PIL 模块实际上是Pillow 模块,是旧的PIL 模块的克隆。 Pillow 模块不是标准 Python 发行版的一部分。
  • 您可以简单地使用self.img = ImageTk.PhotoImage(file=...) 替换两个self.img = ... 语句。
  • 是的,我同意你的 cmets 并修复了我的代码 @acw1688
  • 感谢您的宝贵时间和回答。我使用了符号反斜杠,因为我输入了 r' --- ' 这应该是插入路径的语法,但我可能错了。无论如何我试过了,它仍然只显示画布的背景颜色,白色。如果你想知道的话,这张图片是一张巨大的 OK 用手淹死的照片。
  • 我也尝试使用 PIL 模块插入图像,但仍然无法正常工作。当我使用 PIL 或 PhotoImage 运行程序时,没有错误消息...
猜你喜欢
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 2021-03-30
相关资源
最近更新 更多