Need help making a random image choser in tkinter Python

Question:

hello so im trying to make a random image chooser , my images are in the same folder as the script and what im trying to do is get a number from 0-6 then chose that image, all the images are titled 0.png, 1.png,2.png etc, btw im fairly new to python

this is the code

from tkinter import *
import random
from PIL import ImageTk,Image
root = Tk()

root.geometry("400x400")
gen1=random.randint(0,6)

my_img = ImageTk.PhotoImage(Image.open(gen1".png"))
my_label = Label(image=my_img)
my_label.pack()
root.mainloop()

gen1 is generating a random number and then a bit lower putting .png to it
it is simply not working can somebody explain?

Asked By: P0larr

||

Answers:

open(gen1".png")

This won’t work. Perhaps try open(str(gen1) + ".png")) to stringify your random integer and concatenate it with the extension.

Answered By: dig dug
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.