0

I am trying to select a photo from my computer and open it in my program. I can search and select it safely wihout importing ImageTk,Image but when I import the ImageTk,Image the program is not even opening. Is there another way? If there is not can you tell me an alternative of ImageTk,Image. I tried to uninstall Pillow and install back. I tried Pillow 7.0.0 and 8.0.0 both of theese supposed to work with my python 3.8.5, I couldn't find or understand the problem. I tried to use both conda and original python paths from Vscode, nothing changed. I don't know but maybe libraries are blocking themselves etc. If you know anything this is important for me. Thx from now.

***Problem section:***


from os import defpath
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from PIL import ImageTk,Image



bruh_filename = filedialog.askopenfilename(initialdir = "D:/steam_apps")
windows2 = Toplevel()
my_image = ImageTk.PhotoImage(bruh_filename)
my_image_label = Label(windows2, Image = my_image).pack()

#########################################

***Full Code:***

from os import defpath
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from PIL import ImageTk,Image


root = Tk()
root.geometry("400x400")
root.iconbitmap("favicon.ico")
root.title("NSDMR.X")



#im2 = PhotoImage(file="giphy.gif")
#back = Label(root, image=im2)
#back.pack()

username = "Enes"
age = "19"
blood = "0Rh+"
password = "Bruh"

label1 = Label(root, text="hoş geldiniz.")
label1.pack()

username_l = Label(root, text="Lütfen kullanıcı adını girin.")
username_l.pack()
response = Entry(root)
response.pack()

age_l = Label(root, text="Lütfen yaşınızı girin.")
age_l.pack()
response2 = Entry(root)
response2.pack()

blood_l = Label(root, text="Lütfen kan grubunuzu girin.")
blood_l.pack()
response3 = Entry(root)
response3.pack()

password_l = Label(root, text="Lütfen parolayı girin.")
password_l.pack()
response4 = Entry(root)
response4.pack()


MODES = [
   ("Rocky Road to London.", "Rocky Road to London."),
   ("Rocky Road to Dublin.", "Rocky Road to Dublin."),
   ("Rocky Road to Mexico.", "Rocky Road to Mexico."),
   ]

Rocky = StringVar()
#Rocky.set("Rocky Road to Mexico.")

for text, mode in MODES:
   Radiobutton(root, text=text, variable=Rocky, value=mode).pack()
#ImageTk.PhotoImage

def loginbruh():
   if  response.get() == username and response2.get() == age and response3.get() == blood and response4.get() == password and Rocky.get() == "Rocky Road to Dublin.":
      messagebox.showinfo("Bilgilendirme", "Doğru girdiniz.")
      bruh_filename = filedialog.askopenfilename(initialdir = "D:/steam_apps")
      windows2 = Toplevel()
      my_image = ImageTk.PhotoImage(file=bruh_filename)
      print(bruh_filename)
      #my_image_label = Label(windows2, image = my_image).pack()
      
   else:
      messagebox.showwarning("Bilgilendirme", "Yanlış girdiniz. Programdan çıkışınız yapılıyor!")
      root.quit() 

   
login_button = Button(root, text="Giriş", command=loginbruh)
login_button.pack()

im = PhotoImage(file="red_cross.png")

exit_button = Button(root, image = im, width=64, height=64, command=root.quit)
exit_button.pack()

root.mainloop()
Nsdmr.x
  • 11
  • 3
  • 1
    ```ImageTk.PhotoImage(file=bruh_filename)```. You need a ```file``` argument. Also, you have an indentation error –  Jun 13 '21 at 16:34
  • @Sujay The indetation is correct in the original code, I edited this too right now thx. I wrote the `file` but still now working. I tried it before many times btw – Nsdmr.x Jun 13 '21 at 16:44
  • Did you consider printing the ```bruh_filename```? –  Jun 13 '21 at 16:46
  • @Sujay how can I integrate to my program? because I dont know how to print except for console. Btw its in tkinter with frames, buttons vs. I am not working in console I mean – Nsdmr.x Jun 13 '21 at 16:50
  • @Nsdmr.x `Image=my_image` should actually be `image=my_image` – TheLizzard Jun 13 '21 at 16:53
  • ```print(bruh_filename)``` ill give you the value of the ```bruh_filename```. When you don't get your expected output, you need to debug and narrow down the bug. If it prints nothing then your problem is found. And if it prints something then the problem is with the code –  Jun 13 '21 at 16:53
  • @Sujay I tried it. It printed the path of photo so the code is working I guess. It says AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo' – Nsdmr.x Jun 13 '21 at 16:58
  • @TheLizzard thx man I didn't notice it. I changed it but nothing different. – Nsdmr.x Jun 13 '21 at 16:59
  • 1
    @TheLizzard pointed out your problem. ```image=my_image``` is correct. ```Image``` is not a parameter for the ```tk.Label``` –  Jun 13 '21 at 16:59
  • 1
    did you create ```root=Tk()``` and ```root.mainloop()```. Just curious to know if you created a main loop –  Jun 13 '21 at 17:01
  • @Sujay created both bro, If I dont add ImageTk and anything from it to my program, it actually works. 50 lines of code works without it but I really need to use it. – Nsdmr.x Jun 13 '21 at 17:02
  • 1) `my_image=ImageTk.PhotoImage(Image.open(bruh_filename))` 2) `Label(windows2, image=my_image).pack()` – 8349697 Jun 13 '21 at 17:03
  • Post, the full code. I will take a look –  Jun 13 '21 at 17:03
  • @Sujay did it under the main problem code, comment section was too short for it – Nsdmr.x Jun 13 '21 at 17:06
  • @8349697 tried it, still now showing the photos... – Nsdmr.x Jun 13 '21 at 17:08
  • Umm. I question? Why Have you defined ```Toplevel()``` before ```root=Tk()``` –  Jun 13 '21 at 17:08
  • @Sujay I didn't bro, I seperated cods with ###... first one is not working section and the second is full code – Nsdmr.x Jun 13 '21 at 17:10
  • ```bruh_filename = filedialog.askopenfilename(initialdir = "D:")```, ```windows2 = Toplevel()```, ```my_image = ImageTk.PhotoImage(file=bruh_filename)```, ```my_image_label = Label(windows2, image = my_image).pack()```. Add this after ```root.title()``` –  Jun 13 '21 at 17:11
  • @Sujay Already like that, examine the code, root comes first. – Nsdmr.x Jun 13 '21 at 17:13
  • You've updated your code. What kind of error are you getting? Traceback? – 8349697 Jun 13 '21 at 17:14
  • [ImageTk.PhotoImage](https://pillow.readthedocs.io/en/stable/reference/ImageTk.html#PIL.ImageTk.PhotoImage) does not accept a file path. – 8349697 Jun 13 '21 at 17:16
  • It does. @8349697. I load images with ```ImageTk``` –  Jun 13 '21 at 17:18
  • @8349697 AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo' – Nsdmr.x Jun 13 '21 at 17:27
  • @8349697 and now ImportError: DLL load failed while importing _imaging: – Nsdmr.x Jun 13 '21 at 17:39
  • @Nsdmr.x And that's another question. Determine what the main problem is, describe your situation in the question, update the code if necessary, and add full `Traceback (most recent call last)` to your post. Otherwise, the discussion will go in the wrong direction. – 8349697 Jun 13 '21 at 17:56
  • @8349697 I found the problem, when I gave a path directly, it opens it but when some variable holds path inside, its not opening it. I opened a new project, uninstalled the PIL and pillow and installed the pillow again, ImageTk is now working but like I said. I need to fix this – Nsdmr.x Jun 13 '21 at 18:02
  • By the way, your code works, but it doesn't show the image in the popup. But if you use this life hack with [an additional reference to the image](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function), then everything works. – 8349697 Jun 13 '21 at 18:26
  • `bruh_filename = filedialog.askopenfilename(initialdir = "D:/...")`, `my_image = ImageTk.PhotoImage(Image.open(bruh_filename))`, `my_image_label = Label(windows2, image=my_image)`, `my_image_label.image = my_image`, `my_image_label.pack()` – 8349697 Jun 13 '21 at 18:38
  • @8349697 wow dude! it works. Thx a lot, you saved the day! You can answer the question by this code. – Nsdmr.x Jun 13 '21 at 19:28

0 Answers0