I'm trying to make a dice so each image is the picture of a face of a dice and I want it to randomly pick one of the images using a button then replace the image its printed on the screen with another random image when the button is click
So when I run the module it pops up with one of the 6 random images but as soon as i click the button 'roll' it doesn't change the image to another random image but makes the image disappear. I'm wanting the variable 'myimg' to change its value depending on the 'number' variable, out come then replaces the image printed on the tkinter window to then known 'myimg' value by using the button.
import random
import tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("Dice")
window.geometry("400x400")
window.configure(background='white')
count= 1
while count == 1:
number = random.randint(1,6)
if number == 1:
myimg = "1.jpg"
count = 0
elif number == 2:
myimg = "2.jpg"
count = 0
elif number == 3:
myimg = "3.jpg"
count = 0
elif number == 4:
myimg = "4.jpg"
count = 0
elif number == 5:
myimg = "5.jpg"
count = 0
elif number == 6:
myimg = "6.jpg"
count = 0
def update_the_picture():
updated_picture = ImageTk.PhotoImage(Image.open(myimg))
w.configure(image = updated_picture)
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expect an image object.
img = ImageTk.PhotoImage(Image.open(myimg))
#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
w = tk.Label(window, image = img)
b = tk.Button(window, text="Role Dice", command = update_the_picture)
#The Pack geometry manager packs widgets in rows or columns.
w.pack(side = "bottom", fill = "both", expand = "yes")
b.pack()
count= 1
#Start the GUI
window.mainloop()