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

时间:2020-03-13 20:33:28

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

我一直在使用tkinter和python 3编写应用程序。我已经创建了一个画布,并且正在尝试在其上显示一个5000x5000像素的gif图像,其中该画布是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')

我想知道是否有任何解决方案,以防万一,请告诉我。谢谢您的时间!

1 个答案:

答案 0 :(得分:2)

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

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