LYF511 发表于 2019-3-29 20:24:14

python报错

请问怎么解决?

报错:

Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
    app = App(root)
File "<pyshell#4>", line 13, in __init__
    self.igmlabel = tk.Label(root, image=photo)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2763, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2296, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage3" doesn't exist

代码:


import tkinter.filedialog as tf
import tkinter as tk
import PIL.ImageGrab as PI
import sys
import time


class App:
    def __init__(self, root):

      root = tk.Tk()

      photo = PI.grab()
      photo.save(r"test.gif")
      time.sleep(2)

      photo = tk.PhotoImage(file=r"test.gif")

      self.igmlabel = tk.Label(root, image=photo)
      self.igmlabel.pack(side=LEFT)
      
      self.button = tk.Button(root, text="保存", command=self.save)
      self.button.pack(side=RIGHT)

      self.button = tk.Button(root, text="保存", command=self.save)
      self.button.pack(side=RIGHT)
      
      def save(self):
            file = tf.asksaveasfilename(title="保存", filetypes=[("PNG", ".png"), ("JPG", ".jpg"), ("GIF", ".gif")])

      
root = tk.Tk()
root.title("截图")
root.geometry()
app = App(root)

root.mainloop()


请问怎么解决?

一个账号 发表于 2020-3-7 22:10:49

https://blog.csdn.net/cat7102/article/details/79177489(求采纳)
页: [1]
查看完整版本: python报错