2015-12-28 34 views
1

我想做一个简单的计算器,并且我希望在我的按钮上有一个条目小部件,但网格不工作。Tkinter条目和网格

这是我的代码:

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

如何使程序的按钮排队与输入框,同时还采用网格管理器?

+2

尝试进行以下更改并让我知道您是否希望如此。 'e = Entry(root,width = w * 6); 实例(行= 1,列= 1,列距= 4)' – Reti43

+0

看看计算器的例子[这里](http://zetcode.com/gui/tkinter/layout/)。它似乎是做你想做的。 –

+0

谢谢你们解决了我的问题!节日快乐,并有一个很好的编程年! –

回答

1

简单的解决方案是让您的入口小部件跨越所有四列。

e.grid(row=1, column=1, columnspan=4) 
+0

谢谢你的帮助! –