如果
Button显示文本,则在使用
height和
width选项时,它们的单位以文本为单位。为了使它们方形,使用像素单位会更好。为此,您需要将该按钮放置在中
frame,并确保框架不会传播(
grid_propagate),并允许其子元素填充(
columnconfigure&
rowconfigure)。
这只是一个示例,因为我看不到您的代码。
import Tkinter as tkmaster = tk.Tk()frame = tk.frame(master, width=40, height=40) #their units in pixelsbutton1 = tk.Button(frame, text="btn")frame.grid_propagate(False) #disables resizing of frameframe.columnconfigure(0, weight=1) #enables button to fill frameframe.rowconfigure(0,weight=1) #any positive number would do the trickframe.grid(row=0, column=1) #put frame where the button should bebutton1.grid(sticky="wens") #makes the button expandtk.mainloop()
编辑: 我刚刚看到您的编辑(添加您的代码)。将相同的内容应用于代码后;
import Tkinter, tkFonttop = 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()



