user4442238
user4442238

Reputation:

tkinter: change image in window to random image from variable

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()

Upvotes: 0

Views: 4813

Answers (2)

user4171906
user4171906

Reputation:

Note that the image "updated_picture" is local to the function so is garbage collected as soon as the function update_the_picture() exits and that is fast enough that is seems like it doesn't show. You have to make the images persistent. One way of several is to open them once and store them in a list, instead of every time the function is called.

window = tk.Tk()
window.title("Dice")
window.geometry("400x400")
window.configure(background='white')

def update_the_picture():
    num=random.randint(0, 5)
    w.configure(image = images[num])

images=[]
for fname in range(1,7):
    img = ImageTk.PhotoImage(Image.open("%d.jpg" % (fname)))
    images.append(img)

w = tk.Label(window, image = img)
w.pack(side = "bottom", fill = "both", expand = "yes")

b = tk.Button(window, text="Role Dice", command = update_the_picture).pack()

update_the_picture()

window.mainloop()

Upvotes: 0

fhdrsdg
fhdrsdg

Reputation: 10532

Your while loop does not work as you expect it to. When code is executed it does not execute again unless specifically called. Resetting count to 1 does not do that. So instead of using that while loop, you should define a function which picks a new image to show. Try this:

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')

def new_img():
    # Pick a new number
    number = random.randint(1,6)
    # Add '.jpg' to number
    myimg = str(number)+'.jpg'
    # Return the image name
    return myimg

def update_the_picture():
    # Get the new image name
    myimg = new_img()
    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.
myimg = new_img()
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()

#Start the GUI
window.mainloop()

Upvotes: 2

Related Questions