如果我正在删除条目小部件,一切都会完美调整,但在添加条目小部件后,所有内容都会被扰乱。我正在使用网格几何管理器,我已经指定了输入框的位置,但它仍然无法正确显示。
from Tkinter import*
root=Tk()
root.title("Calculator")
def calculate():
print x
def callback(x):
print x
Entry(root).grid(row=0)
Button(root,text="0",command=lambda:callback(0)).grid(row=4,column=1)
Button(root,text="1",command=lambda:callback(1)).grid(row=1,column=0)
Button(root,text="2",command=lambda:callback(2)).grid(row=1,column=1)
Button(root,text="3",command=lambda:callback(3)).grid(row=1,column=2)
Button(root,text="4",command=lambda:callback(4)).grid(row=2,column=0)
Button(root,text="5",command=lambda:callback(5)).grid(row=2,column=1)
Button(root,text="6",command=lambda:callback(6)).grid(row=2,column=2)
Button(root,text="7",command=lambda:callback(7)).grid(row=3,column=0)
Button(root,text="8",command=lambda:callback(8)).grid(row=3,column=1)
Button(root,text="9",command=lambda:callback(9)).grid(row=3,column=2)
Button(root,text="+",command=lambda:callback(12)).grid(row=1,column=3)
Button(root,text="-",command=lambda:callback(13)).grid(row=2,column=3)
Button(root,text="*",command=lambda:callback(14)).grid(row=3,column=3)
Button(root,text="/",command=lambda:callback(15)).grid(row=4,column=3)
Button(root,text="=",command=calculate).grid(row=4,column=2)
Button(root,text=".",command=lambda:callback(11)).grid(row=4,column=0)
root.mainloop()
答案 0 :(得分:1)
它是网格 - 当您在列中添加Entry
时,同一列中的其他单元格具有相同的宽度。您可以使用columnspan为Entry
tk.Entry(root).grid(row=0, columnspan=4)
您可以使用sticky='we'
将按钮调整为单元格宽度。
import Tkinter as tk
# --- functions ---
def calculate():
print x
def callback(x):
print x
# --- main ---
root = tk.Tk()
root.title("Calculator")
tk.Entry(root).grid(row=0, columnspan=4)
tk.Button(root, text="0", command=lambda:callback(0)).grid(row=4, column=1, sticky='we')
tk.Button(root, text="1", command=lambda:callback(1)).grid(row=1, column=0, sticky='we')
tk.Button(root, text="2", command=lambda:callback(2)).grid(row=1, column=1, sticky='we')
tk.Button(root, text="3", command=lambda:callback(3)).grid(row=1, column=2, sticky='we')
tk.Button(root, text="4", command=lambda:callback(4)).grid(row=2, column=0, sticky='we')
tk.Button(root, text="5", command=lambda:callback(5)).grid(row=2, column=1, sticky='we')
tk.Button(root, text="6", command=lambda:callback(6)).grid(row=2, column=2, sticky='we')
tk.Button(root, text="7", command=lambda:callback(7)).grid(row=3, column=0, sticky='we')
tk.Button(root, text="8", command=lambda:callback(8)).grid(row=3, column=1, sticky='we')
tk.Button(root, text="9", command=lambda:callback(9)).grid(row=3, column=2, sticky='we')
tk.Button(root, text="+", command=lambda:callback(12)).grid(row=1, column=3, sticky='we')
tk.Button(root, text="-", command=lambda:callback(13)).grid(row=2, column=3, sticky='we')
tk.Button(root, text="*", command=lambda:callback(14)).grid(row=3, column=3, sticky='we')
tk.Button(root, text="/", command=lambda:callback(15)).grid(row=4, column=3, sticky='we')
tk.Button(root, text="=", command=calculate).grid(row=4, column=2, sticky='we')
tk.Button(root, text=".", command=lambda:callback(11)).grid(row=4, column=0, sticky='we')
root.mainloop()
编辑:更短
import Tkinter as tk
# --- functions ---
def callback(x):
print x
# --- main ---
buttons = [
("1", "2", "3", "+"),
("4", "5", "6", "-"),
("7", "8", "9", "*"),
(".", "0", "=", "/"),
]
root = tk.Tk()
root.title("Calculator")
tk.Entry(root).grid(row=0, columnspan=4)
for r, row in enumerate(buttons, 1):
for c, text in enumerate(row):
b = tk.Button(root, text=text, command=lambda arg=text:callback(arg))
b.grid(row=r, column=c, sticky='we')
root.mainloop()