我正在创建一个有关“生活游戏”的动画,我想制作按钮或类似的东西来控制该动画。
我已经用Tkinter进行了搜索,但是找不到适合我的问题的东西,我也搜索了如何使用myplotlib中的funcAnimation制作动画,但是我无法使其工作
def测试(M,n)
#Test if there are still turns to be done else it stop
if n == 0:
return
#Close the window after a break
plt.pause(3)
plt.close('all')
#Find the width and lenght of the matrix since it can be rectangle
l = len(M[0])
L = len(M)
#The matrix with the next gen
M2 = []
#Adding enought lines to avoid errors
for k in range (L):
M2.append([])
#Double for loops to scan all cases in the original Matrix
for Largeur in range (L):
for longueur in range (l):
#Counter of neighbors
c = 0
#Counting the neighbors around
for i in range (3):
for j in range (3):
if not (i == 1 and j == 1):
try:
c += M[Largeur-i+1][longueur-j+1]
except:
pass
#Placing 0 or 1 depending on neighbors around in the new matrix
if M[Largeur][longueur] == 0:
if c == 3:
M2[Largeur].insert(longueur, 1)
else:
M2[Largeur].insert(longueur, 0)
else:
if c == 2 or c == 3:
M2[Largeur].insert(longueur, 1)
else:
M2[Largeur].insert(longueur, 0)
#Replace the current values of the graph to "animate"
im.set_array(M2)
#Cooldown because otherwise it's instant and no animation
plt.pause(0.10)
#Launch the next gen
Test(M2, n-1)
当我尝试放置tkinter按钮时,它们直到动画完成后才会显示