Home > Net >  Need help making a random image choser in tkinter Python
Need help making a random image choser in tkinter Python

Time:09-07

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?

CodePudding user response:

open(gen1".png")

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

  • Related