I understand that you can make a button that can do some actions when clicked with Tkinter, but how can I just make a button that turns from one color to another when clicked? Then, from that, how do I replicate that button to make a grid of them? I would also settle for a grid of buttons that just change from one character to another.
import Tkintercolor="red"
default_color="white"def main(n=10):window = Tkinter.Tk()last_clicked = [None]for x in range(n):for y in range(n):b = Tkinter.Button(window, bg=default_color, activebackground=default_color)b.grid(column=x, row=y)# creating the callback with "b" as the default parameter bellow "freezes" its value pointing# to the button created in each run of the loop.b["command"] = lambda b=b: click(b, last_clicked)return windowdef click(button, last_clicked):if last_clicked[0]:last_clicked[0]["bg"] = default_colorlast_clicked[0]["activebackground"] = default_colorbutton["bg"] = colorbutton["activebackground"] = colorlast_clicked[0] = buttonw = main()
Tkinter.mainloop()