0

New to programming. Learning Python. Trying to make dice rolling simulator with actual imitation of the roll itself. The code below works fine, except that it only shows one dice (alays the last one in code) out of three planned. All important variables are in global, and no errors are ahown when running.

Using Labels not an option because transparent background becomes visible.

Any suggestions how to make it work?

from tkinter import *
import random, time

def sides():
    global side
    d1 = PhotoImage(file=('d1.png'))
    d2 = PhotoImage(file=('d2.png'))
    d3 = PhotoImage(file=('d3.png'))
    d4 = PhotoImage(file=('d4.png'))
    d5 = PhotoImage(file=('d5.png'))
    d6 = PhotoImage(file=('d6.png'))
    side = random.choice([d1,d2,d3,d4,d5,d6])
    return side

def roll():
    global dice_l, dice_c, dice_r
    for i in range(15):
        dice_l = canvas.create_image(100,250,image=sides())
        dice_c = canvas.create_image(250,250,image=sides())
        dice_r = canvas.create_image(400,250,image=sides())
        window.update()
        time.sleep(0.1)

window = Tk()
window.title('DiceRollSim')
window.resizable(height=False, width=False)
window.iconphoto(True, PhotoImage(file=('icon.png')))

canvas = Canvas(window, width=500, height=500)
canvas.pack()

bgrd = PhotoImage(file=('board.png'))
canvas.create_image(250,250,image=bgrd)

throw = Button(window, text='ROLL', command=roll)
throw.place(relx=0.5, rely=0.9, anchor=S)

roll()

window.mainloop()
DimUA
  • 1
  • 1
  • See [this answer](https://stackoverflow.com/questions/75193479/display-all-png-files-of-your-current-folder-with-tkinter/75193826#75193826) – Collaxd Jan 24 '23 at 14:43
  • 1
    The linked question doesn't answer my question. I'm not using classes and did put all significant variables to global. – DimUA Jan 24 '23 at 15:25

0 Answers0