Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need help making a random image choser in tkinter Python

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

open(gen1".png")

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading