-1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
johnkhigginson
  • 123
  • 1
  • 14
  • 1
    As in the for loop, tkinter mainloop does not take control to update the changes. Add `self.update()` inside the end of for loop block to force the update. – acw1668 Feb 29 '20 at 08:32
  • 1
    First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) – stovfl Feb 29 '20 at 09:05
  • Generally speaking, you shouldn't call `time.sleep()` in tkinter applications. You should instead use the universal widget [`after()`](https://web.archive.org/web/20190222214221id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) method to schedule calls to a function that updates the images. There a numerous examples of doing that in related questions here about animation in tkinter if you look for them. – martineau Feb 29 '20 at 10:18

1 Answers1

0

As tkinter does not take control during the for loop, so the changes cannot be updated. Add self.update() at the end of the for loop block to force the update:

for i in range(20):
    ...
    self.update()
acw1668
  • 40,144
  • 5
  • 22
  • 34