我正在尝试制作一个简单的计算器,并且希望在按钮上有一个输入小部件,但是网格无法正常工作。
这是我的代码:
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()
这是我的输出:输出
在仍然使用网格管理器的情况下,如何使程序的按钮与输入框对齐?
一种简单的解决方案是让您的输入小部件跨越所有四列。
e.grid(row=1, column=1, columnspan=4)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句