为什么图像没有显示在 canvas 上? [Python3 + tkinter]

Why the the image isn't displayed on the canvas? [Python3 + tkinter]

我一直在使用 tkinter 和 python 编写应用程序 3. 我创建了一个 canvas,我想在上面显示一个 5000x5000 像素的 gif 图像,其中canvas 是2000x2000 像素,但是在运行 程序运行时图像不出现。这是代码:

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')

请问有没有解决办法,如果有请告诉我。感谢您的宝贵时间!

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

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()