_tkinter.TclError:图像“ \ full \ directory \ link.gif”不存在

时间:2018-12-12 11:19:18

标签: python-3.x tkinter python-imaging-library

对不起,我很痛苦,我知道有上帝知道还有很多其他问题涉及此“图像不存在” tkinter错误,但是,我发现的所有问题似乎都没有整个目录的问题,它们似乎都只是在引用文件名。

因此,我正在尝试进行徽标测验,(我从示例中删除了一些不相关的代码,因为否则在这里放置的时间太长了。)

但是,当我尝试显示徽标时,它告诉我该图像不存在。 向我显示了整个目录链接的问题,如果我直接从错误中复制并放置到Windows资源管理器中,则可以很好地打开图像。

代码如下:

from tkinter import *
import tkinter.messagebox
import random
from PIL import Image, ImageTk
import os

class Window(Frame):

    global questionstart
    def questionstart():
        if gameplayvar == 0:
            chosenlinknumber = random.randint(0, len(templist)-1)
            chosen = templist[chosenlinknumber]
            temp = chosen.split("\\")
            temp2 = temp[2].split(".")
            temp3 = temp2[0].split("L")

            global directory
            quizlogo_label = Label(root, image = directory)
            quizlogo_label.grid(row=1, column=2)

root = Tk()
root.geometry("900x590") 
root.iconbitmap(r'\\replacing\the\server\directory\Documents\pythontests\Logos\logoquizlogo_YVp_icon.ico')

C = Canvas(root, bg="blue", height=500, width=500)

possiblelinks = [
                '\\NewLogos\AdobeLogo.gif',
                '\\NewLogos\AmazonLogo.gif',
                '\\NewLogos\AppleLogo.gif',
                '\\NewLogos\BMWLogo.gif',
                '\\NewLogos\ChromeLogo.gif',
                '\\NewLogos\DebianLogo.gif',
                '\\NewLogos\DiscordLogo.gif',
                '\\NewLogos\FirefoxLogo.gif',
                '\\NewLogos\GoogleLogo.gif',
                '\\NewLogos\MicrosoftLogo.gif',
                '\\NewLogos\OperaLogo.gif',
                '\\NewLogos\PhotoshopLogo.gif',
                '\\NewLogos\PlayStationLogo.gif',
                '\\NewLogos\PythonLogo.gif',
                '\\NewLogos\TwitterLogo.gif',
                '\\NewLogos\VisualStudioCodeLogo.gif',
                '\\NewLogos\WindowsLogo.gif'
            ]

templist = possiblelinks

chosenlinknumber = random.randint(0, len(templist)-1)
chosen = templist[chosenlinknumber]

p1 = '\\replacing\the\server\directory\Documents\pythontests'
directory = p1+chosen

app = Window(root)

root.mainloop()

这是我运行它时返回的内容:

"C:\Program Files (x86)\Python36-32\python.exe" "//replacing/the/server/directory/Documents/pythontests/tkinterimagetry1.py"
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "//replacing/the/server/directory/Documents/pythontests/tkinterimagetry1.py", line 143, in quizstart
    questionstart()
  File "//replacing/the/server/directory/Documents/pythontests/tkinterimagetry1.py", line 121, in questionstart
    quizlogo_label = Label(root, image = directory)
  File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 2760, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 2293, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "\\replacing\the\server\directory\Documents\pythontests\NewLogos\DiscordLogo.gif" doesn't exist

我不知道是什么原因导致它无法找到文件,因为由于错误而正在分发的目录在Windows资源管理器中可以正常运行。

谢谢,

1 个答案:

答案 0 :(得分:0)

这是有问题的部分:

global directory
quizlogo_label = Label(root, image = directory)

Label构造函数希望将图像作为ImageTk实例而不是包含文件名的字符串传递。

它必须类似于:

global directory
self.photo = Image.PhotoImage(directory)
self.image = ImageTk.PhotoImage(self.photo)
quizlogo_label = Label(root, image = self.photo)

请注意,图像类的两个实例都被分配为self实例的Window属性,因此在函数退出时不会被垃圾回收