Search code examples
pythontkinterphotoimage

I can't change the image using label.configure


I am trying to create a GUI in which I can process images, so I have to change the default image to the one chosen by the browse button. The default image disappears but the new image doesn't appear. Help Please! Here is my code:

from Tkinter import *
from tkFileDialog import askopenfilename
import cv2

class Browse_image :
    def __init__ (self,master) :

        frame = Frame(master)
        frame.grid(sticky=W+E+N+S)
        self.browse = Button(frame, text="Browse", command = lambda: self.browseim())
        self.browse.grid(row=13, columnspan=2)
        self.check = Checkbutton(frame, text="On/Off")
        self.check.grid(row=0)
        self.maxval = Scale(frame, from_=0, to=100, orient=HORIZONTAL)
        self.maxval.grid(row=1,columnspan=2)
        self.minval = Scale(frame, from_=0, to=100, orient=HORIZONTAL)
        self.minval.grid(row=2,columnspan=2)
        self.photo = PhotoImage(file="browse.png")
        self.label = Label(frame, image=self.photo)
        self.label.grid(row=3,rowspan=10)

    def browseim(self):
        path = askopenfilename(filetypes=(("png files","*.png"),("jpeg files","*.jpeg")) )
        if path:
            self.photo = PhotoImage(path)                        
            self.label.configure(image = self.photo)
            #self.label.image = self.photo
            #self.label.grid(row=3,rowspan=10)


root= Tk()
b= Browse_image(root)
root.mainloop()

Solution

  • Change self.photo = PhotoImage(path) to self.photo = PhotoImage(file=path). The file parameter is required to define an image path in PhotoImage class.