falcon user
falcon user

Reputation: 767

Tkinter Entry and Grid

I am trying to make a simple calculator, and I want to have an entry widget over my buttons, but the grid is not working out.

This is my code:

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()

And this is my output: Output

How do I make the program's buttons line up with entry box while still using grid manager?

Upvotes: 1

Views: 930

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

The simple solution is to have your entry widget span all four columns.

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

Upvotes: 1

Related Questions