0

Hi I would appreciate if you told what's wrong with this code? I wanted to change the label image when button is pressed. After I press the button it just shows blank white image. Any help appreciated MY code:

def browseFiles(): 
    filename = filedialog.askopenfilename(initialdir = "C:/Users/Osman/VSC/python/", title = "Select 
    a File", filetypes = (("Image files", ("*.jpg*","*.png"),), ("all files","*.*"))) 
    fff=Image.open(filename)
    fff=fff.resize((230,250),Image.ANTIALIAS)
    temp_img=ImageTk.PhotoImage(fff)
    label_blank.configure(image=temp_img)

default=Image.open('blank.png')
default=default.resize((300,250),Image.ANTIALIAS)
filename_blank=ImageTk.PhotoImage(default)
label_blank=tk.Label(win,image=filename_blank)
label_blank.image=filename_blank
label_blank.place(x=130,y=150,height=230,width=250)

button_explore = tk.Button(win,text = "Select input Image", command = browseFiles)
button_explore.place(x=200,y=400)
  • `fff` is a local variable to the `browseFiles` function and is cleaned up once that function has executed. You need to keep hold of the Image after. The answer I have linked to suggests using a global (there are better ways though) – scotty3785 Jan 06 '21 at 14:23
  • Thanks man, feel so dumb now – Osman Mashadov Jan 07 '21 at 09:44

0 Answers0