为什么按钮没有正确对齐?

Why doesn't the buttons align up properly?

所以,我一直在尝试用Tkinter设计一个简单的计算器GUI。但我面临着这个我无法解决的奇怪问题。这是我的代码,我希望它不会看起来很乱。

from tkinter import *

root = Tk()

display = Entry(root,width=48,borderwidth=5)
display.grid(row=0,column=0,columnspan=3)

button_1 = Button(root, text="1",width=16,height=3)
button_2 = Button(root, text="2",width=16,height=3)
button_3 = Button(root, text="3",width=16,height=3)
button_4 = Button(root, text="4",width=16,height=3)
button_5 = Button(root, text="5",width=16,height=3)
button_6 = Button(root, text="6",width=16,height=3)
button_7 = Button(root, text="7",width=16,height=3)
button_8 = Button(root, text="8",width=16,height=3)
button_9 = Button(root, text="9",width=16,height=3)
button_0 = Button(root, text="0",width=16,height=3)

button_clear = Button(root, text="Clear",width=32,height=3)
button_plus = Button(root, text="+",width=16,height=3)
button_equal = Button(root, text="=",width=32,height=3)

button_1.grid(row=3,column=0)
button_2.grid(row=3,column=1)
button_3.grid(row=3,column=2)

button_4.grid(row=2,column=0)
button_5.grid(row=2,column=1)
button_6.grid(row=2,column=2)

button_7.grid(row=1,column=0)
button_8.grid(row=1,column=1)
button_9.grid(row=1,column=2)

button_0.grid(row=4,column=0)

button_clear.grid(row=4,column=1,columnspan=2)
button_plus.grid(row=5,column=0)
button_equal.grid(row=5,column=1,columnspan=2)

root.mainloop()

这是 GUI 的屏幕截图

First Problem:为什么 clearequal 按钮与 369 按钮不对齐, 看看按钮的 right margin ,它怎么没有正确对齐。

我已经将按钮 3、6、9 的宽度设置为 16,而对于 clearequal 按钮,我将其设置为两倍 32,并且这些也很清晰等号按钮有 column-span 列,共 2 列。

Second Problem:这是次要问题,因为它不会使 GUI 看起来难看。为什么上面的entry widget和下面number buttonswidth不匹配,入口小部件的宽度设置为48,个人thrice纽扣。

提前致谢!

问题是按钮太小,无法占据 2 列,而且 tkinter 不会自动将它们拉伸。您必须使用 sticky 属性 手动配置拉伸,以便它知道拉伸的方向:button_clear.grid(row=4,column=1,columnspan=2, sticky='we')。这是您修复的完整代码:

from tkinter import *

root = Tk()

display = Entry(root,width=48,borderwidth=5)
display.grid(row=0,column=0,columnspan=3)

button_1 = Button(root, text="1",width=16,height=3)
button_2 = Button(root, text="2",width=16,height=3)
button_3 = Button(root, text="3",width=16,height=3)
button_4 = Button(root, text="4",width=16,height=3)
button_5 = Button(root, text="5",width=16,height=3)
button_6 = Button(root, text="6",width=16,height=3)
button_7 = Button(root, text="7",width=16,height=3)
button_8 = Button(root, text="8",width=16,height=3)
button_9 = Button(root, text="9",width=16,height=3)
button_0 = Button(root, text="0",width=16,height=3)

button_clear = Button(root, text="Clear",width=32,height=3)
button_plus = Button(root, text="+",width=16,height=3)
button_equal = Button(root, text="=",width=32,height=3)

button_1.grid(row=3,column=0)
button_2.grid(row=3,column=1)
button_3.grid(row=3,column=2)

button_4.grid(row=2,column=0)
button_5.grid(row=2,column=1)
button_6.grid(row=2,column=2)

button_7.grid(row=1,column=0)
button_8.grid(row=1,column=1)
button_9.grid(row=1,column=2)

button_0.grid(row=4,column=0)

button_clear.grid(row=4,column=1,columnspan=2, sticky='we')
button_plus.grid(row=5,column=0)
button_equal.grid(row=5,column=1,columnspan=2, sticky='we')

root.mainloop()

希望对您有所帮助!

我根据上面做了一些修改,也好看了。

试试这个:

from tkinter import *

root = Tk()

display = Entry(root,width=33,borderwidth=5, font = ('Helvetica',15, 'bold'))
display.grid(row=0,column=0,columnspan=3,  ipady =10)

button_1 = Button(root, text="1",width=16,height=3, bd =5)
button_2 = Button(root, text="2",width=16,height=3, bd =5)
button_3 = Button(root, text="3",width=16,height=3, bd =5)
button_4 = Button(root, text="4",width=16,height=3, bd =5)
button_5 = Button(root, text="5",width=16,height=3, bd =5)
button_6 = Button(root, text="6",width=16,height=3, bd =5)
button_7 = Button(root, text="7",width=16,height=3, bd =5)
button_8 = Button(root, text="8",width=16,height=3, bd =5)
button_9 = Button(root, text="9",width=16,height=3, bd =5)
button_0 = Button(root, text="0",width=16,height=3, bd =5)

button_clear = Button(root, text="Clear",width=32,height=3, bd =5)
button_plus = Button(root, text="+",width=16,height=3, bd =5)
button_equal = Button(root, text="=",width=32,height=3, bd =5)

button_1.grid(row=3,column=0)
button_2.grid(row=3,column=1)
button_3.grid(row=3,column=2)

button_4.grid(row=2,column=0)
button_5.grid(row=2,column=1)
button_6.grid(row=2,column=2)

button_7.grid(row=1,column=0)
button_8.grid(row=1,column=1)
button_9.grid(row=1,column=2)

button_0.grid(row=4,column=0)

button_clear.grid(row=4,column=1,columnspan=2, sticky = 'we')
button_plus.grid(row=5,column=0)
button_equal.grid(row=5,column=1,columnspan=2, sticky = 'we')

root.mainloop()

输出: