所以,我一直在尝试使用GUI
设计一个简单的计算器Tkinter
。但是我面临着这个无法解决的怪异问题。这是我的代码,希望它看起来不会混乱。
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()
First Problem
:为什么clear
和equal
按钮与3
,6
,9
按钮不对齐,只是看一下按钮的right margin
,没有正确对齐。
我将按钮3、6、9的宽度设置为16
,将clear
和equal
按钮的宽度设置为32
的两倍,并且这些清晰且相等的按钮的column-span
为2
列。
Second Problem
:这是第二个问题,因为它不会使GUI看起来很丑。为什么顶部的entry widget
与下面的width
的number buttons
不匹配,所以输入小部件的宽度设置为48
,thrice
各个按钮。
提前感谢!
所以,我一直在尝试使用Tkinter设计一个简单的计算器GUI。但是我面临着这个无法解决的怪异问题。这是我的代码,希望它看起来不会混乱。来自tkinter import * root = ...
问题是按钮实际上太小,无法占用2列,tkinter
不会自动将它们拉长。您必须使用sticky
属性手动配置拉伸,以便它知道拉伸的方向:button_clear.grid(row=4,column=1,columnspan=2, sticky='we')
。这是固定的完整代码: