0

I am trying to get a random card image from my "images" folder to appear on the screen. I have tried the code below but it will not work.

from tkinter import *
from PIL import Image, ImageTk
import random
import os


root = Tk()
root.title("Blackjack")
canvas = Canvas(root, bg="green", height=800, width=1200)
canvas.pack(expand=True)


def shuffle_cards():
    # Get list of all images in the images folder
    card_images = os.listdir("images")
    # Randomly select a card image
    random_card = random.choice(card_images)
    # Create image from the selected card
    img = ImageTk.PhotoImage(Image.open(random_card))
    # Update the canvas with the new card
    canvas.create_image(0, 0, image=img, anchor="nw")


shuffle_btn = Button(root, text="Shuffle", command=shuffle_cards)
canvas.create_window(500, 500, window=shuffle_btn)

root.mainloop()

I have tried this also, img = ImageTk.PhotoImage(Image.open("images/" + random_card))

rioV8
  • 24,506
  • 3
  • 32
  • 49
abcdan
  • 1

0 Answers0