当“按预期”进行操作时,为什么会出现此错误?

时间:2018-11-20 23:08:15

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

我有以下代码:

from tkinter import *
from PIL import Image, ImageTk

class Imageviewer:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        self.previousButton = Button(frame, text = "Previous",
                                     command = lambda:print("hello2"))
        self.previousButton.pack()

        self.nextButton = Button(frame, text = "Next Image",
                                 command = lambda:print("Sziavagyok"))
        self.nextButton.pack()

        photo = Image.open('./kepek/hello(1).png')
        imag = ImageTk.PhotoImage(photo)
        self.Kep = Label(master, image = imag)
        self.Kep.image = imag
        self.Kep.pack()


    def printMessage(self):
        print("Helloka")

root = Tk()
viewer = Imageviewer(root)
root.mainloop()

当我尝试运行它时,出现此错误:

Traceback (most recent call last):
  File "C:/Users/gergo/PycharmProjects/untitled/hello.py", line 34, in <module>
    viewer = imag.Imageviewer(root)
  File "C:\Users\gergo\PycharmProjects\untitled\imageviewer.py", line 18, in __init__
    self.Kep = Label(frame, image = imag)
  File "C:\Users\gergo\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\gergo\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage10" doesn't exist

我尝试了无处不在的书写方式,但是仍然无法正常工作……有什么帮助吗?

0 个答案:

没有答案