I am creating a simple Dice generator using BreezyPythonGUI. I got the whole thing to work, but now I am attempting to create a rolling or flipping animation before the dice roll. You will likely understand what I am attempting to do by reading under the generate function.
'''Dice rolling generator'''
import random
from breezypythongui import EasyFrame
import time
from tkinter import PhotoImage
#from tkinter.font import Font
class DiceGenerator(EasyFrame):
def __init__(self):
EasyFrame.__init__(self, "Dice Generator")
self.setResizable(True)
self.setBackground('#6D8DB9')
self.addLabel(text = 'Dice Generator', row = 0,
column = 0, columnspan = 2, background = '#6D8DB9')
self.die1 = self.addLabel(text = '-', row = 1,
column = 0, sticky = 'E',
foreground = '#FFFFFF', background = '#6D8DB9')
self.die2 = self.addLabel(text = '-', row = 1,
column = 1, sticky = 'W',
foreground = '#FFFFFF', background = '#6D8DB9')
self.image1 = PhotoImage(file = 'd6.png')
self.image2 = PhotoImage(file = 'd6.png')
self.die1["image"] = self.image1
self.die2["image"] = self.image2
# self.die1.configure(font = 14)
# self.die2.configure(font = 14)
self.generate = self.addButton(text = 'Go!', row = 2,
column = 0, columnspan = 2,
command = self.generate)
self.generate.configure(width = 10)
def generate(self):
'This is where I am attempting the animation'
for i in range(20):
x1 = random.randint(1, 6)
y1 = random.randint(1, 6)
time.sleep(.1)
self.image1.configure(file = 'd' + str(x1) + '.png')
self.image2.configure(file = 'd' + str(y1) + '.png')
'The final rolled dice correctly display after this following code.'
x = random.randint(1, 6)
y = random.randint(1, 6)
self.image1.configure(file = 'd' + str(x) + '.png')
self.image2.configure(file = 'd' + str(y) + '.png')
def main():
DiceGenerator().mainloop()
if __name__ == '__main__':
main()
When I run the generate()
function, it pauses for the sleeping time times 20 (because of range(20)
) and then displays the rolled dice. Instead of going through the animation it just acts like it is frozen.
What do I do to create this animation?