Py学习  »  Python

试图解决python 3.5中“image”对象没有属性“name”的问题

Rocky • 5 年前 • 2069 次点击  

我正在尝试为应用程序构建一个GUI。当我将代码用作函数时,以下代码工作正常。它没有显示错误。但是当我在课堂上使用它的时候,这就变得疯狂了。它不断抛出错误:图像对象没有属性名或其他属性名。我不知道怎么解决。有人能帮我吗?谢谢

from PIL import Image as Img
from wand.image import Image
import uuid
import numpy as np
import glob
import os
import sys

from tkinter import filedialog
from tkinter.filedialog import askopenfilename 
from tkinter import *
import tkinter as tk   




class TkFileDialogExample(tk.Frame):

    def __init__(self, root):
        tk.Frame.__init__(self, root)
        button_opt = {'fill': tk.constants.BOTH, 'padx': 10, 'pady': 10}
        tk.Button(self, text='Individual files', command=self.askopenfilename).pack(**button_opt)
        self.file_opt = options = {}
        options['defaultextension'] = '.txt'
        options['filetypes'] = [('all files', '.*'), ('files', '.pdf')]
        options['initialdir'] = 'C:\\Users\\myfolder\\Pictures'
        options['parent'] = root


    def askopenfilename(self):
        filename = filedialog.askopenfilename(**self.file_opt)
        filename_temp = str(filename)[1:-2]
        print(filename_temp)

        if len(filename_temp) == 0: 
            result = Label(text="Err.. : file not selected", fg="red").pack(padx=105, pady=2)
        else:

            filepdf = filename_temp[1:-1]
            filepdf = filepdf.replace('/', '\\')
            try:
                print('file name is ',filepdf)
                with Image(filename=filepdf, resolution=200) as img:
                    img.compression_quality = 99
                    #save it to tmp name
                    img.save(filename='C:\\Users\\myfolder\\Pictures\\%s.jpeg') 
            except Exception as err:
                print(err)
                return False
        result = Label(text="Done Successfully !" , fg="blue").pack(padx=105, pady=2)
        return   result


if __name__ == '__main__': 
    root = tk.Tk()
    TkFileDialogExample(root).pack()
    Label(text="Log : ", fg="blue").pack(padx=100, pady=5)
    root.mainloop()

这是我运行代码时得到的错误:

Exception ignored in: <bound method Image.__del__ of <tkinter.Image object at 0x0000000007799F28>>
    Traceback (most recent call last):
      File "c:\Python3\lib\tkinter\__init__.py", line 3499, in __del__
        if self.name:
    `AttributeError: 'Image' object has no attribute 'name''
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38634
 
2069 次点击  
文章 [ 1 ]  |  最新文章 5 年前