If you have any questions, comments or issues with this project please post them here!
Hi Ian! I installed the pillow-pil package from the āpackagesā tab, but, even after installing it, I get an import error whenever I run the āmain.pyā file. I get the below error message:
Traceback (most recent call last):
File āmain.pyā, line 2, in
from PIL import Image, ImageTK
ImportError: cannot import name āImageTKā from āPILā (/home/runner/Day67100Days/venv/lib/python3.8/site-packages/PIL/init.py)
Nvm. Used ChatGPT to figure it out!
Problem was that I used āImageTKā instead of āImageTkā.
Also please note that the video for this day67 incorrectly states to use āImageTKā.
When I run Davidās solution code it says ācontainer is not definedā
yupā¦ rightā¦ the solution is not working, the png files are missing, tkinter seems like a really nice thing to skip -.-
UPDATE
fiddled with it and with the help of chatgpt I got it workingā¦ super weird this tkinterā¦
This took me a long time to figure out. In addition to the problem in the video mentioned above, it also says in the video to use Image.open
like this:
image = tk.PhotoImage(Image.open(".tutorial/Guess Who/charlotte.jpg") )
I ran into two problems with this. The first is I need to change packages from tk (tkinter) to ImageTk
. The second is I can no longer scale my image using .subsample()
. ChatGPT told me to use image.resize()
instead, and apply it in between the two functions above. End code that worked:
image = Image.open(".tutorial/Guess Who/charlotte.jpg")
image = image.resize((image.width // 5, image.height // 5))
image = ImageTk.PhotoImage(image)
Here is what I did and it shows images really nice . Got the idea and help from @Capresis and chatGPT:
import tkinter as tk
from PIL import Image , ImageTk
window = tk.Tk()
window.title("Guess Who")
window.geometry("300x200")
hello = tk.Label(text = "Guess Who")
hello.pack()
def guesswho():
input = text.get("1.0","end").lower().strip()
hello["text"] = "Guess Who"
if input == "charlotte":
canvas.itemconfig(container, image = tk_charlotte)
elif input == "gerald":
canvas.itemconfig(container, image = tk_gerald)
elif input == "katie":
canvas.itemconfig(container, image = tk_katie)
elif input == "mo":
canvas.itemconfig(container, image = tk_mo)
else:
hello["text"] = "Image not found"
text = tk.Text(window ,height = 1 ,width = 25)
text.pack()
button = tk.Button(text = "Find", command = guesswho)
button.pack()
canvas = tk.Canvas(window, width = 300, height = 200)
canvas.pack()
charlotte = Image.open(".tutorial/Guess Who/charlotte.jpg")
resized_charlotte = charlotte.resize((100, 100), Image.LANCZOS)
tk_charlotte = ImageTk.PhotoImage(resized_charlotte)
gerald = Image.open(".tutorial/Guess Who/gerald.jpg")
resized_gerald = gerald.resize((100, 100), Image.LANCZOS)
tk_gerald = ImageTk.PhotoImage(resized_gerald)
katie = Image.open(".tutorial/Guess Who/katie.jpg")
resized_katie = katie.resize((100, 100), Image.LANCZOS)
tk_katie = ImageTk.PhotoImage(resized_katie)
mo = Image.open(".tutorial/Guess Who/mo.jpg")
resized_mo = mo.resize((100, 100), Image.LANCZOS)
tk_mo = ImageTk.PhotoImage(resized_mo)
container = canvas.create_image(150,50,image=None)
tk.mainloop()
As Dave (and also some of my friends) said we shouldnāt be focusing on mastering tkinter, so I instantly regret spending too much time on these days 66-69