1

so I know you might have read similar questions but my question is just a bit different, I use python 3.8.3 and I know python basics so I wanted to start using GUI, I started off with Tkinter. I had written a small program which does nothing I was just trying but it doesn't work I don't know why. It gives error that unable to find file, I tried doing the code people told in other questions but it doesn't work, probably due to python versions, here is my code:

from tkinter import  *

window= Tk()
window.title("first GUI program of mine")
window.configure(background="black")

photo1 = PhotoImage(file="images.jpg")
Label (window, text="1 or 2 ? :", bg="black", fg="white", font="calibri 12 bold") .grid(row=1, column=0, sticky=W)


windows.mainloop()

so when I run this I get error like this:

tkinter.TclError: couldn't open "images.jpg": no such file or directory

the file is in the same folder in which the python program is saved.

any ideas?

3
  • use this: image = Image.open("images.jpg") photo1 = ImageTk.PhotoImage(image)
    – JacksonPro
    Dec 20, 2020 at 7:34
  • thx for the program but i got this new error when i try it, it's this: AttributeError: type object 'Image' has no attribute 'open'
    – user14676615
    Dec 20, 2020 at 7:40
  • As the error said, the image cannot be found in current directory (it may not be the same directory of the python script). Use print(os.getcwd()) to show the current directory and see whether it is what you expect. Also, JPG is not supported by tkinter.PhotoImage(). Use Pillow module instead.
    – acw1668
    Dec 20, 2020 at 11:10

3 Answers 3

0

Can you show your working directory? I think it's the problem of directory. If it's under the same level of directory, maybe you could try this: '/images.jpg'

1
  • umm so what should i show? should i copy the whole error message?
    – user14676615
    Dec 20, 2020 at 7:55
0

PhotoImage class only supports GIF, PGM, PPM formats. So try to convert it.

5
  • ok i will try by changing extension, btw i tried it before it did not work
    – user14676615
    Dec 20, 2020 at 7:56
  • ok it gave same error again: AttributeError: type object 'Image' has no attribute 'open'
    – user14676615
    Dec 20, 2020 at 7:57
  • the only thing which happens is the tkinter window opens, it is full of black colour background, just for informing that when i delete code for image it works.
    – user14676615
    Dec 20, 2020 at 7:58
  • check out this link c-sharpcorner.com/blogs/…. I hope it helps
    – user14740330
    Dec 20, 2020 at 8:11
  • thaks alot it helped but it did not actually perform all operations i wanted................ just a request can u tell me how to resize the window through the program i typed in Q instead of the new program u told? cuz actually that new program doesn't allow me to rename the window title
    – user14676615
    Dec 20, 2020 at 9:52
0

That worked for me

from tkinter import  *
from PIL import Image, ImageTk

window= Tk()
window.title("first GUI program of mine")
window.configure()

photo1 = Image.open('images.jpeg')
photo1.show()

Label(window, text="1 or 2 ? :", bg="black", fg="white", font="calibri 12 bold").grid(row=1, column=0, sticky=W)

window.mainloop()

And where do you want the image to be shown? To show the image use show() function.

7
  • ok i will try, errrrr the module messed up.................
    – user14676615
    Dec 20, 2020 at 9:54
  • there is no module found it says :( i have been facing this problem from long time
    – user14676615
    Dec 20, 2020 at 9:55
  • Did you have all modules installed? Use pip to install PIL module. Dec 20, 2020 at 11:34
  • how do i?? i just started using GUI and tkinter i have no idea how to
    – user14676615
    Dec 21, 2020 at 5:00
  • idk how to install modules, if u can pls tell
    – user14676615
    Dec 21, 2020 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.