0
from tkinter import *
from tkinter.filedialog import askopenfile

def uploadImg():
    f = askopenfile(filetypes=[('PNG','*.png'),("JPG",'.jpg'),('JPEG','*.jpeg')])
    img = PhotoImage(file=f.name)
    l.config(image=img,width=img.width(),height=img.height())
    btn.place_forget()

    # func() # After adding an error line the code works perfectly. (After uncommenting this code shows the image)


root = Tk()
root.config(
    padx=50,
    pady=50
)
l = Label(master=root,width=100,height=20,borderwidth=1,relief='solid')
l.pack()

btn = Button(master=root,text='Choose A File',command=uploadImg)
btn.place(x=320,y=160)
root.mainloop()

I want my code to show the image on window but it is not.

My code doesn't show the image until I make any error in the uploadImg function. Please uncomment the line "func() # After ..." for this code to work

This is the first time I faces this type of error, To solve this error I have to make any error in my code.

If it's the device problem, Plz tell me in the comment.

codester_09
  • 5,622
  • 2
  • 5
  • 27
  • 2
    Strange. tkinter is a python interface to the C based tcl/tk. It seems like this interface doesn't add a reference to `image`. The image update to label happens after your routine exits but by then the image has been deleted. `func()` raises an error and I think your image stays alive because of the traceback stack trace. Saving img anywhere should work. – tdelaney Aug 07 '22 at 17:34
  • https://stackoverflow.com/a/3482156/642070 – tdelaney Aug 07 '22 at 17:36

2 Answers2

1

I get my answer just add line l.image = img after l.config(...) line.So, it's working fine.

from tkinter import *
from tkinter.filedialog import askopenfile

def uploadImg():
    f = askopenfile(filetypes=[('PNG','*.png'),("JPG",'.jpg'),('JPEG','*.jpeg')])
    img = PhotoImage(file=f.name)
    l.config(image=img,width=img.width(),height=img.height())
    l.image = img
    btn.place_forget()


root = Tk()
root.config(
    padx=50,
    pady=50
)
l = Label(master=root,width=100,height=20,borderwidth=1,relief='solid')
l.pack()

btn = Button(master=root,text='Choose A File',command=uploadImg)
btn.place(x=320,y=160)
root.mainloop()

codester_09
  • 5,622
  • 2
  • 5
  • 27
  • `l.imgae` - spelling error – tdelaney Aug 07 '22 at 17:21
  • @tdelaney Lol, But it's working with this also without any error. – codester_09 Aug 07 '22 at 17:23
  • Interesting - this answer https://stackoverflow.com/a/3482156/642070 says that all that matters is you put it somewhere so there is a python level reference to the image and its not deleted when the function returns. I tried a global called "foo" and it worked too. I think in the OP's case, a reference is in the traceback stack frame. – tdelaney Aug 07 '22 at 17:30
  • 1
    Looking around a bit more, this is a bug in python's tkinter but its been there forever. Your solution of `l.image = img` is good - especially if its used consistently throughout code. This code will naturally get rid of old `l.image` when a new one is assigned or when `l` itself is deleted. – tdelaney Aug 07 '22 at 17:41
0

After l.config, you should add this and it will work (make your tweaks how you want afterwards):

l.createImage(10,
    10,
    anchor=NW,
    image=img)