example.py
def setnumber():
    column = -1
    row = 0
    root = tk.Tk()
    root.title('numbers')
    root.geometry('470x310')
    for i in range(101):
        if i > 0:
            if i%10 == 1:
                row += 1 
                column = -1
            column += 1
            text=f'{i}'
            btn = tk.Button(root, text=text)
            btn.grid(column=column, row=row)
            btn.config(command=collback(btn))
    root.mainloop()
def collback(btn):
    def nothing():
        btn.config(bg='#008000')
    return nothing
First, I am making buttons from 1 to 100 at setnumber (). The reason why it looks a little confusing is that when I made 10 pieces, I made a line break. Finally, specify collback (btn) as the callback function and pass the variable btn as an argument. In that function, call a function called nothing to change the color.
When you do this, only the button pressed will turn green as shown below.

Recommended Posts