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

Why does my FOR loop in Python's time module only display the last random image selected instead of random images every time?

I am working on the bare bones of a slot machine game for a coding class and I am running into an issue with the function I am making for the slot rolling. The FOR loop is supposed to pick a random image, wait some time, do that 3 times and then move on.

What happened was that the image only rolls after the entire loop is done. It doesn’t change the image 3 times, it changes it once after the loop is done looping. How do I get it to look like it is actually looping 3 times?

this issue is in the roll_slots function

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

from tkinter import *
from tkinter import ttk
import time
import random as ran

game = Tk()
game.title("Slots Game")
game.geometry('1600x800')
Mango = PhotoImage(file="Mango-PNG.png")
Banana = PhotoImage(file="Banana-Fruit-PNG.png")
Blackberry = PhotoImage(file="Blackberry-Fruit-PNG-Image.png")
Apple = PhotoImage(file="Red-Apple-PNG-HD.png")


def navigate_to_game():
    menus.select(1)


# this is the meat and potatoes, this is what happens when you  roll the slots
def roll_slots():
    list1 = [Apple, Banana, Blackberry, Mango]

    for i in range(3):
        time.sleep(1)
        x = ran.choice(list1)
        slot1.configure(image=x)
        time.sleep(1)

    # a = ran.choice(list1)
    # b = ran.choice(list1)
    # c = ran.choice(list1)
    # slot1.configure(image=a)
    # slot2.configure(image=b)
    # slot3.configure(image=c)
    # return a, b, c


# menu creation (using the notebook widgets)
menus = ttk.Notebook(game)

mainmenu = Frame(menus)
menus.add(mainmenu, text="Main Menu")
gamemenu = Frame(menus)
menus.add(gamemenu, text="Machine")
menus.pack(expand=1, fill="both")

# main menu programming
welcome = Label(mainmenu, text="This Is The Game", font=("Terminal", 20))
welcome.pack(pady=40)

coin = PhotoImage(file="Coins.png")
mainmenucoin = Label(mainmenu, image=coin)
mainmenucoin.pack()

playgame = Button(mainmenu, text="Start Gambling!", command=navigate_to_game)
playgame.pack()


# actual gameplay coding
slotframe = Frame(gamemenu)
slotframe.pack()

slot1 = Label(slotframe, image=Mango)
slot1.pack(side=LEFT)
slot2 = Label(slotframe, image=Mango)
slot2.pack(side=LEFT)
slot3 = Label(slotframe, image=Mango)
slot3.pack(side=LEFT)

gamble = Button(gamemenu, text="gamble", command=roll_slots)
gamble.pack()

game.mainloop()

>Solution :

Modify the roll_slots() function as follows:

def roll_slots():
    list1 = [Apple, Banana, Blackberry, Mango]
    for i in range(3):
        game.after(100)
        x = ran.choice(list1)
        slot1.configure(image=x)
        game.after(100)
        game.update()

To see the delay effect in tkinter, use game.after(ms) instead of time.sleep(1).

And update the entire window. game.after(100) #100 is 0.1s

Hope this helps.

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