我正在尝试制作一个简单的计算器,并且我希望在我的按钮上有一个条目小部件,但是网格没有用完。
这是我的代码:
from tkinter import *
w = 6
h = w - 3
root = Tk()
root.title("Calculator")
def callback():
print("pressed")
e = Entry(root)
e.grid(row=1, column=1)
b1 = Button(root, bg='white', width=w, height=h, text="1", activebackground="black", activeforeground="white", command=callback)
b1.grid(row=2, column=1)
b2 = Button(root, bg='white', width=w, height=h, text="2", activebackground="black", activeforeground="white", command=callback)
b2.grid(row=2, column=2)
b3 = Button(root, bg='white', width=w, height=h, text="3", activebackground="black", activeforeground="white", command=callback)
b3.grid(row=2, column=3)
b4 = Button(root, bg='white', width=w, height=h, text="4", activebackground="black", activeforeground="white", command=callback)
b4.grid(row=3, column=1)
b5 = Button(root, bg='white', width=w, height=h, text="5", activebackground="black", activeforeground="white", command=callback)
b5.grid(row=3, column=2)
b6 = Button(root, bg='white', width=w, height=h, text="6", activebackground="black", activeforeground="white", command=callback)
b6.grid(row=3, column=3)
b7 = Button(root, bg='white', width=w, height=h, text="7", activebackground="black", activeforeground="white", command=callback)
b7.grid(row=4, column=1)
b8 = Button(root, bg='white', width=w, height=h, text="8", activebackground="black", activeforeground="white", command=callback)
b8.grid(row=4, column=2)
b9 = Button(root, bg='white', width=w, height=h, text="9", activebackground="black", activeforeground="white", command=callback)
b9.grid(row=4, column=3)
b0 = Button(root, bg='white', width=w, height=h, text="0", activebackground="black", activeforeground="white", command=callback)
b0.grid(row=5, column=2)
AC = Button(root, bg='white', width=w, height=h, text="AC", activebackground="black", activeforeground="white", command=callback)
AC.grid(row=5, column=1)
plus = Button(root, bg='white', width=w, height=h, text="+", activebackground="black", activeforeground="white", command=callback)
plus.grid(row=2, column=4)
minus = Button(root, bg='white', width=w, height=h, text="-", activebackground="black", activeforeground="white", command=callback)
minus.grid(row=3, column=4)
mult = Button(root, bg='white', width=w, height=h, text="×", activebackground="black", activeforeground="white", command=callback)
mult.grid(row=4, column=4)
div = Button(root, bg='white', width=w, height=h, text="÷", activebackground="black", activeforeground="white", command=callback)
div.grid(row=5, column=4)
equ = Button(root, bg='white', width=w, height=h, text="=", activebackground="black", activeforeground="white", command=callback)
equ.grid(row=5, column=3)
root.mainloop()
这是我的输出: Output
在使用网格管理器的同时,如何使程序的按钮与输入框对齐?
答案 0 :(得分:1)
简单的解决方案是让您的条目小部件跨越所有四列。
e.grid(row=1, column=1, columnspan=4)