1

I have an image in a label and I want that image to be changed when I press the button but instead of changing the image the window becomes blank:

from tkinter import *
from PIL import Image,ImageTk
import os
root = Tk()
root.state("zoomed")
def chng():
    photo2 = ImageTk.PhotoImage(Image.open("upload.jpg"))
    img.config(image = photo2) 
    img.grid() 
photo = ImageTk.PhotoImage(Image.open("cat.jpg"))
img = Label(root,image = photo)
upload = Button(root, text= "Upload" ,height = 3, width = 12, command = 
chng)
upload.grid( )
for col_num in range(root.grid_size()[1]):
    root.columnconfigure(col_num, minsize=600)
for row_num in range(root.grid_size()[1]):
    root.rowconfigure(row_num, minsize=60)
img.grid() 
root.mainloop()
TemporalWolf
  • 7,162
  • 1
  • 30
  • 46
Fahad
  • 23
  • 1
  • 7
  • 2
    Possible duplicate of [Tkinter Label does not show Image](https://stackoverflow.com/questions/13148975/tkinter-label-does-not-show-image) – stephan Nov 20 '17 at 20:07
  • 3
    To add to my close suggestion: `photo2` gets deleted on function exit. To fix your sample code, keep a reference to your image (eg by adding `global photo2` at the beginning of the definition of `chng`). – stephan Nov 20 '17 at 20:09

3 Answers3

6

You have to keep a reference to the image.

def chng():
    photo2 = ImageTk.PhotoImage(Image.open("upload.jpg"))
    img.config(image = photo2) 
    img.photo_ref = photo2 # keep a reference

And you don't need the extra grid() call.

Novel
  • 12,855
  • 1
  • 22
  • 38
0

Try this:

def chng():
    photo2 = ImageTk.PhotoImage(Image.open("upload.jpg"))
    img.config(image = photo2) 
    img.grid()
    root.update_idletasks()
giantas
  • 1,313
  • 14
  • 25
  • root.mainloop() after root.update() worked for me. thankx for help. – Fahad Nov 20 '17 at 19:52
  • 3
    @Fahad: If your program calls `mainloop()` more than once, you're doing something wrong. – Bryan Oakley Nov 20 '17 at 19:53
  • i guess you should edit your question or this answer to reflect the solution for future users – giantas Nov 20 '17 at 19:53
  • root.update_idletasks() isn't working too. if u have another solution i am open to suggestions. – Fahad Nov 20 '17 at 19:59
  • and now there is another problem it is not coming out of this mainloop so code after it is not running. – Fahad Nov 20 '17 at 20:00
  • @Fahad `mainloop` is working as designed - it won't return until the root window is destroyed. This is why you don't want to call it more than once. – Bryan Oakley Nov 20 '17 at 20:24
0
import Tkinter as tk
from PIL import Image

class Application(tk.Frame):
    def __init__(self, master=None):
    tk.Frame.__init__(self, master)
    self.grid(row=5,column=2)
    self.createWidgets()
    top = self.winfo_toplevel()
def createWidgets(self):
    self.grid()

app = Application()
simge = tk.PhotoImage(file="bg.png")
hira1 = tk.Label(image=simge)
hira1.grid(row=0,column=0,rowspan=5)

def manu1(event):
    simen1=tk.PhotoImage(file="bg1.png")
    hira1.configure(image=simen1)
    hira1(image=simen1)

hira1.bind("<Button 1>",manu1)
app.mainloop()