dimanche 29 mars 2020

How to randomize images with tkinter and pillow?

in an attempt to develop a simple program I ran into a problem

the app should display a random image but its only randomized when I run the code not when i press the button to randomize it

import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
import random

root = tk.Tk()

Images = ["Image.png", "Image1.png", "Image2.png", "Image3.png"]

Canvas = tk.Canvas(root, bg="Blue", height=500, width=500)
Canvas.pack()

def Next():
    Random_Image=random.choice(Images)
    print(Random_Image)

    Random_Image_Original = Image.open(Random_Image)
    Random_Image_To_Resize = Random_Image_Original.resize((250, 250))
    Random_Image_Resize = ImageTk.PhotoImage(Random_Image_To_Resize)
    return Random_Image_Resize

Random_Image_Resize=Next()
Displayed_Image = Label(Canvas,  image=Random_Image_Resize)
Displayed_Image.place(height=250, width=250, y=125, x=125)

Button = tk.Button(root, text="Next", padx=20, pady=10, command=Next)
Button.pack()

root.mainloop()

if i try printing the output it does print random image names




Aucun commentaire:

Enregistrer un commentaire