I am attempting to animate the series of button presses in the simon game I am creating with tkinter. The animate method is called for each button that needs to be pressed in the series. It currently presses all of the buttons at the same time rather than in a series as it is meant to.
from tkinter import *
from random import choice
root = Tk()
Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
frame = Frame(root)
frame.grid(row=0, column=0, sticky=N+E+S+W)
def animate(btn):
btn.config(relief=SUNKEN, state=ACTIVE)
btn.after(200, lambda: btn.config(relief=RAISED, state=NORMAL))
class Game(object):
def __init__(self):
self.colors = ["green", "red", "yellow", "blue"]
self.i = 0
self.round = 1
self.moves = []
self.cpu_moves = []
self.btns = []
for row in range(2):
Grid.rowconfigure(frame, row, weight=1)
for col in range(2):
Grid.columnconfigure(frame, col, weight=1)
btn = Button(frame, width=150, height=150, bg=self.colors[0],
command=lambda b=self.i: self.user_select(b))
btn.grid(row=row, column=col)
self.btns.append(btn)
self.colors.remove(self.colors[0])
self.i += 1
def user_select(self, btn):
self.moves.append(btn)
print(self.moves)
print(self.cpu_moves)
if self.moves == self.cpu_moves:
self.clear_user_moves()
self.round += 1
self.cpu_move()
elif len(self.moves) == len(self.cpu_moves) and self.moves != self.cpu_moves:
self.clear_cpu_moves()
def clear_user_moves(self):
self.moves = []
def clear_cpu_moves(self):
self.cpu_moves = []
def cpu_move(self):
nums = [0, 1, 2, 3]
self.cpu_moves.append(choice(nums))
print(self.cpu_moves)
for i in self.cpu_moves:
root.after(200, animate(self.btns[i]))
class App(object):
def __init__(self, master):
self.master = master
game = Game()
game.cpu_move()
root.geometry("300x300")
app = App(root)
root.resizable(width=False, height=False)
root.mainloop()