How to upgrade image in a tkinter label when pressing a button

时间:2019-04-16 22:04:12

标签: python-3.x tkinter

I'm trying to update the image of a tkinter label each time I press a button. I've searched the site and tried some of the answers here but I'm unable to make the image change. I've already implemented the lab.configure(image=photoK) and put the reference to avoid garbage collection with lab.image=photoK, but the image won't update.

I'm using python 3.7.3 and tkinter version 8.6

    import tkinter as tk
    from tkinter import Label #get a widget
    from tkinter import *
    from tkinter import ttk
    from PIL import Image, ImageTk

    window= Tk()
    window.geometry("500x500")
    window.title("Trabajo1")


    image= Image.open("/Users/rociocortesg/Desktop/criptografia/entrega1/codigoNumero/MasterShare.jpg")
    image = image.resize((250, 250), Image.ANTIALIAS)
    photo= ImageTk.PhotoImage(image)

    lab=Label(image=photo)

    lab.pack(side="bottom", fill="both", expand="yes")

    def upload():
        imageK = Image.open("/Users/rociocortesg/Desktop/criptografia/entrega1/codigoNumero/slave0.jpg")
        imageK = image.resize((250, 250), Image.ANTIALIAS)
        photoK = ImageTk.PhotoImage(imageK)
        #lab=Label(image=photo)
        lab.configure(image=photoK)
        lab.image=photoK

    button1= Button(window,text="Upload",fg="brown", bg= "yellow" , relief= RIDGE, font=("arial",20, "bold"),command=upload)
    button1.place(x=10,y=53)

    widget=Label(None,text="Image Change") #make a Label
    widget.pack()
    widget.mainloop()

0 个答案:

没有答案