我正在尝试从与我的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()
我得到了
from PIL import Image, ImageTK
ImportError: cannot import name 'ImageTK'
提前感谢您的帮助!
答案 0 :(得分:12)
对于Debian / Ubuntu:
Python 2
array([[ 20.57733218, 0.09794384, 0.44497735],
[ 0.87061284, 20.10253493, 0.56643557],
[ 0.76358739, 0.44902046, 20.86064797]])
Python 3
sudo apt-get install python-imaging python-pil.imagetk
对于Archlinux:
sudo apt-get install python3-pil python3-pil.imagetk
它将安装软件包,您可以使用它:sudo pacman -S python-pillow
答案 1 :(得分:1)
答案 2 :(得分:1)
您将必须像这样更改代码:
import PIL
from PIL import ImageTk
from PIL import Image
应该工作正常!
答案 3 :(得分:1)
问题是你命名错误。
ImageTK 代替 ImageTk
保持 k 小,它会起作用。
-MohsinK-
答案 4 :(得分:0)
对于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
答案 5 :(得分:0)
当一起导入TkImage和Image时,Python确实起作用了。您需要先导入PIL,然后再分别导入TkImage和Image,如下所示-
import PIL
from PIL import TkImage
from PIL import Image
这应该工作正常。您还可以通过使用以下命令提示符来检查枕头是否正确安装在系统中-
import PIL
答案 6 :(得分:-1)
sudo apt-get install python3-pil.imagetk
对我有用!
答案 7 :(得分:-3)
弄清楚了! 您必须单独导入它们而不是一行。
from PIL import Image
from PIL import ImageTk
的Insead
from PIL import Image, ImageTk