If a Button
displays text, when you use height
and width
options, their units in text unit. To make them square, using pixel unit would be better. To do that, you need to place that button in a Frame
and make sure frame won't propagate(grid_propagate
) and allow its children to fill it (columnconfigure
& rowconfigure
).
This is just an example, since I don't see your code.
import Tkinter as tk
master = tk.Tk()
frame = tk.Frame(master, width=40, height=40) #their units in pixels
button1 = tk.Button(frame, text="btn")
frame.grid_propagate(False) #disables resizing of frame
frame.columnconfigure(0, weight=1) #enables button to fill frame
frame.rowconfigure(0,weight=1) #any positive number would do the trick
frame.grid(row=0, column=1) #put frame where the button should be
button1.grid(sticky="wens") #makes the button expand
tk.mainloop()
EDIT: I just saw your edit(adding your code). After applying same things to your code;
import Tkinter, tkFont
top = Tkinter.Tk()
right = Tkinter.Frame(top)
right.pack(side = "right")
font = tkFont.Font(family="Helvetica", size=20, weight = tkFont.BOLD)
for i in xrange(6):
f = Tkinter.Frame(right,width=50,height=50)
b = Tkinter.Button(f, text = str(i), font = font)
f.rowconfigure(0, weight = 1)
f.columnconfigure(0, weight = 1)
f.grid_propagate(0)
f.grid(row = i/3, column = i%3)
b.grid(sticky = "NWSE")
top.mainloop()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…