Button parameter “command” executes when declared, but not when pressed
<p>I want to change the color of the button when its pressed.</p>

<pre><code>map = []

for y in range(20):
    for x in range(20):
        map.append(0)

def button_map():
    btn_c = ""
    if map[x+20*y] == 1:
        btn_c ="red"
    elif map[x+20*y] == 2:
        btn_c ="blue"
    elif map[x+20*y] == 0:
        btn_c ="orange"
    return btn_c

def button_map_set(x, y):
    if map[x+20*y] == 1:
        map[x+20*y] = 0
    elif map[x+20*y] == 0:
        map[x+20*y] = 1

main = Tk()
frame1 = Frame(main)
frame1.pack()
for y in range(20):
    for x in range(20):
</code></pre>

<hr>

<p>I have tested this</p>

<pre><code>        def test():
            button_map_set(x, y)
</code></pre>

<hr>

<p>this</p>

<pre><code>        test = button_map_set(x, y)
</code></pre>

<hr>

<p>and this</p>

<pre><code>        btn.bind("&lt;btn&gt;", button_map_set(x, y)
</code></pre>

<hr>

<pre><code>        btn = Button(framex, command = test, bg = button_map())
main.mainloop()
</code></pre>

<p>what happens: it executes when declared, but not when pressed</p>