Is it possible to color a button by clicking, while having multiple other buttons [duplicate] Is it possible to color a button by clicking, while having multiple other buttons [duplicate] tkinter tkinter

Is it possible to color a button by clicking, while having multiple other buttons [duplicate]


I hope the following code gives you a good starting point to go ahead with your game.

I have used the same approach of using two for loops to create a 10x10 grid of buttons. Then i have used an click event callback to get the widget that is being clicked upon and then make the selected button blue or perform any operation on that widget.

import tkinter as tkclass Game:    def __init__(self, master):        self.master = master        self.master.resizable(0,0)        # row index        for row in range(10):            # col index            for col in range(10):                self.button = tk.Button(master, width=1, height=1)                self.button.grid(row=row, column=col, padx=8, pady=8, ipadx=10, ipady=4)                self.button.bind("<Button-1>", self.callback)        master.mainloop()    def callback(self, event):        # get the clicked button        clicked_btn = event.widget        btn_at_row_col = clicked_btn.config(bg='blue', state='disabled')if __name__ == "__main__":    root = tk.Tk()    Game(root)  

The output GUI:

This will render a 10x10 grid of buttons.

Then you can click on any button which makes it blue.