无法导入名称“ImageTK”-python 3.5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44835909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot import name 'ImageTK' - python 3.5
提问by Cody Caro
I am trying to load in an image from the same folder as the one my python script is in.
我正在尝试从与我的 python 脚本所在的文件夹相同的文件夹中加载图像。
# create a class called Person
# create init method
# 2 attributes (name, and birthdate)
# create an object from the Person class
from PIL import Image, ImageTK
import datetime
import tkinter as tk
# create frame
window = tk.Tk()
# create frame geometry
window.geometry("400x400")
# set title of frame
window.title("Age Calculator App")
# adding labels
name_label = tk.Label(text="Name")
name_label.grid(column=0, row=0)
year_label = tk.Label(text="Year")
year_label.grid(column = 0, row = 1)
month_label = tk.Label(text="Month")
month_label.grid(column = 0, row = 2)
day_label = tk.Label(text="Day")
day_label.grid(column = 0, row = 3)
# adding entries
name_entry = tk.Entry()
name_entry.grid(column=1, row=0)
year_entry = tk.Entry()
year_entry.grid(column=1, row=1)
month_entry = tk.Entry()
month_entry.grid(column=1, row=2)
day_entry = tk.Entry()
day_entry.grid(column=1, row=3)
def calculate_age():
year = int(year_entry.get())
month = int(month_entry.get())
day = int(day_entry.get())
name = name_entry.get()
person = Person(name, datetime.date(year, month, day))
text_answer = tk.Text(master=window, wrap=tk.WORD, height=20, width=30)
text_answer.grid(column= 1, row= 5)
answer = "{name} is {age} years old!".format(name=person.name, age=person.age())
is_old_answer = " Wow you are getting old aren't you?"
text_answer.insert(tk.END, answer)
if person.age() >= 50:
text_answer.insert(tk.END, is_old_answer)
calculate_button = tk.Button(text="Calculate Age!", command=calculate_age)
calculate_button.grid(column=1, row=4)
class Person:
def __init__(self, name, birthdate):
self.name = name
self.birthdate = birthdate
def age(self):
today = datetime.date.today()
age = today.year - self.birthdate.year
return age
image = Image.open('LockVectorDaily.jpg')
image.thumbnail((100, 100), Image.ANTIALIAS)
photo = tk.PhotoImage(file=image)
label_image = tk.Label(image=image)
label_image.grid(column=1, row=0)
window.mainloop()
I got
我有
from PIL import Image, ImageTK
ImportError: cannot import name 'ImageTK'
Thank you in advance for the help!
预先感谢您的帮助!
回答by medyas
For Debian/Ubuntu:
对于 Debian/Ubuntu:
Python 2
蟒蛇 2
sudo apt-get install python-imaging python-pil.imagetk
Python 3
蟒蛇 3
sudo apt-get install python3-pil python3-pil.imagetk
For Archlinux:
对于 Archlinux:
sudo pacman -S python-pillow
It will install the package and you can use it: from PIL import ImageTk
它将安装软件包,您可以使用它: from PIL import ImageTk
回答by Dan Anderson
For Python3 on Ubuntu 18 I had to uninstall the Python (2) packages then install the Python 3 packages:
对于 Ubuntu 18 上的 Python3,我必须卸载 Python (2) 软件包,然后安装 Python 3 软件包:
apt-get remove python3-pil python3-pil.imagetk python-pil.imagetk python-pil
apt-get install python3-pil.imagetk # Note that python3-pil installed as a dependency
回答by Michael Matta
回答by sandeepselvaraj
You will have to change the code like this:
您必须像这样更改代码:
import PIL
from PIL import ImageTk
from PIL import Image
It should work fine!
它应该工作正常!
回答by Siddesh Pawar
sudo apt-get install python3-pil.imagetk
It worked for me!
它对我有用!
回答by Cody Caro
Got it figure out! You must import them separately and not on one line.
搞清楚了!您必须单独导入它们,而不是在一行中导入。
from PIL import Image
from PIL import ImageTk
Insead of
代替
from PIL import Image, ImageTk