Linked Questions

Popular Questions

Tkinter canvas not updating photo

Asked by At

I am trying to make a photo editing app with tkinter that preforms an action to the uploaded picture on a button press(eg. flips the picture), but when the button is pressed nothing happens. The code that manipulates the picture works, I just can't get the change to show on screen. Full code:

from PIL import Image, ImageTk
import PIL
import numpy as np
import tkinter as tk
from tkinter import *

image ='images.jpg')

img_data = np.array(image)

def button_flip_onClick():
    global canvas
    global image_container
    global img
    image_flipped_data = np.flip(img_data, axis=1)
    image_flipped = Image.fromarray(image_flipped_data)
    img = ImageTk.PhotoImage(image_flipped)


root = tk.Tk()

canvas = tk.Canvas(width=SCREEN_WIDTH, height=SCREEN_HEIGHT)
#Load an image in the script
img = ImageTk.PhotoImage(image)

image_container = canvas.create_image(10,10,anchor='nw',image=img)

button2 = tk.Button(root, text='flip', command= lambda:button_flip_onClick), rely=0.7, relheight=0.15, relwidth=0.2)


I've tried configuring img in the function, putting image_container after button2, configuring the canvas in the function. All resulted in the picture staying the same.

Related Questions