Home > Net >  smaller way of making a button grid with Tkinter
smaller way of making a button grid with Tkinter

Time:06-08

I know Tkinter isn't the best way of doing this but I didn't think this could be the easiest way of doing it, I need it to be able to be increased and decreased in size as well and I don't see how I could do that using this method

b1 = tkinter.Button(master,command=lambda:[colour(b1),character(b1)])
b1.grid(row=0,column=0)
b2 = tkinter.Button(master,command=lambda:[colour(b2),character(b2)])
b2.grid(row=0,column=1)
b3 = tkinter.Button(master,command=lambda:[colour(b3),character(b3)])
b3.grid(row=0,column=2)
b4 = tkinter.Button(master,command=lambda:[colour(b4),character(b4)])
b4.grid(row=0,column=3)
b5 = tkinter.Button(master,command=lambda:[colour(b5),character(b5)])
b5.grid(row=0,column=4)
b6 = tkinter.Button(master,command=lambda:[colour(b6),character(b6)])
b6.grid(row=1,column=0)
b7 = tkinter.Button(master,command=lambda:[colour(b7),character(b7)])
b7.grid(row=1,column=1)
b8 = tkinter.Button(master,command=lambda:[colour(b8),character(b8)])
b8.grid(row=1,column=2)
b9 = tkinter.Button(master,command=lambda:[colour(b9),character(b9)])
b9.grid(row=1,column=3)
b10 = tkinter.Button(master,command=lambda:[colour(b10),character(b10)])
b10.grid(row=1,column=4)
b11 = tkinter.Button(master,command=lambda:[colour(b11),character(b11)])
b11.grid(row=2,column=0)
b12 = tkinter.Button(master,command=lambda:[colour(b12),character(b12)])
b12.grid(row=2,column=1)
b13 = tkinter.Button(master,command=lambda:[colour(b13),character(b13)])
b13.grid(row=2,column=2)
b14 = tkinter.Button(master,command=lambda:[colour(b14),character(b14)])
b14.grid(row=2,column=3)
b15 = tkinter.Button(master,command=lambda:[colour(b15),character(b15)])
b15.grid(row=2,column=4)
b16 = tkinter.Button(master,command=lambda:[colour(b16),character(b16)])
b16.grid(row=3,column=0)
b17 = tkinter.Button(master,command=lambda:[colour(b17),character(b17)])
b17.grid(row=3,column=1)
b18 = tkinter.Button(master,command=lambda:[colour(b18),character(b18)])
b18.grid(row=3,column=2)
b19 = tkinter.Button(master,command=lambda:[colour(b19),character(b19)])
b19.grid(row=3,column=3)
b20 = tkinter.Button(master,command=lambda:[colour(b20),character(b20)])
b20.grid(row=3,column=4)
b21 = tkinter.Button(master,command=lambda:[colour(b21),character(b21)])
b21.grid(row=4,column=0)
b22 = tkinter.Button(master,command=lambda:[colour(b22),character(b22)])
b22.grid(row=4,column=1)
b23 = tkinter.Button(master,command=lambda:[colour(b23),character(b23)])
b23.grid(row=4,column=2)
b24 = tkinter.Button(master,command=lambda:[colour(b24),character(b24)])
b24.grid(row=4,column=3)
b25 = tkinter.Button(master,command=lambda:[colour(b25),character(b25)])
b25.grid(row=4,column=4)

CodePudding user response:

You can use loop to create buttons.

But using lambda in loop you have to use lambda b=button:[colour(b), character(b) to assign correct button


def change(button):
    colour(button)
    character(button)
   
#all_buttons = []

for y in range(5):
    for x in range(5):
        button = tkinter.Button(master)
        button["command"] = (lambda b=button:change(b))
        button.grid(row=y, column=x)
        #all_buttons.append(button)

Normally inside lambda it uses reference to variable button and it gets value from this variable when you click button - but at this moment this variable has last button from loop. Using b=button it creates new variable b for every lambda and it copies value from button to b - so finally every lambda use different value.

  • Related