Python改变按钮的颜色

时间:2014-05-27 00:14:28

标签: python button tkinter

我今天在这里为这项任务提供一些帮助。我需要让我的按钮根据变量" color"的值来改变颜色。所以,如果在这:

M = [
     [0, 0, 0, 0, 0, 1],
     [1, 1, 0, 0, 0, 1],
     [0, 0, 0, 1, 0, 0],
     [0, 1, 1, 0, 0, 1],
     [0, 1, 0, 0, 1, 0],
     [0, 1, 0, 0, 0, 2]
    ]

如果你找到(0,0)按钮必须是白色的,但它没有做任何改变,所以请帮助我。

boton1 = Button(jugar, bg = color)
boton1.place(x=50,y=10)
boton1.config(heigh=2,width=5)

def cambia_colores(M):
    n=len(M)
    m=len(M[0])
    if M != []:
        return cambia_colores_aux(M,n,m,0,0)
    else:
        return "Error"
def cambia_colores_aux(M,n,m,i,j):
    global color
    if j == m:
        return color
    elif M[i][j] == 0:
        color = "blue"

    elif M[i][j] == 1 :
        color = "black"

    elif M[i][j] == 2:
       color = "red"

    else:
        return cambia_colores_aux(M,n,m,i,j+1)   
    jugar.mainloop()

1 个答案:

答案 0 :(得分:0)

要更改小部件的颜色,您必须调用configure方法。您不能只更改创建窗口小部件时使用的变量。

例如:

...
new_color = cambia_colores(...)
boton1.configure(background=new_color)
...
相关问题