2015-12-28 36 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

謝謝你的幫助! –