Linear Data Structure
Linear Data Structure

Reputation: 342

Grid and Frames in Tkinter

Can we place grid of buttons inside a Frame or can we use both frame and pack inside one class?

I wanted to make a simple GUI based calculator in python like this.

required output

P.S I'm new to python GUI. Assuming my Basics are not clear

This is what i got by using only Grids

Grid output

Code

from tkinter import *
root = Tk()
root.title("Calculator")
Entry(root).grid(row=0, column=0)

one = Button(
        root,
        text="1",
        width=5,
        height=1
             ).grid(row=1, column=1)

two = Button(
        root,
        text="2",
        width=5,
        height=1
             ).grid(row=1, column=2)
three = Button(
        root,
        text="3",
        width=5,
        height=1
             ).grid(row=1, column=3)
four = Button(
        root,
        text="4",
        width=5,
        height=1
             ).grid(row=1, column=4)
five = Button(
        root,
        text="5",
        width=5,
        height=1
             ).grid(row=2, column=1)
six = Button(
        root,
        text="6",
        width=5,
        height=1
             ).grid(row=2, column=2)
seven = Button(
        root,
        text="7",
        width=5,
        height=1
             ).grid(row=2, column=3)
eight = Button(
        root,
        text="8",
        width=5,
        height=1
             ).grid(row=2, column=4)
nine = Button(
        root,
        text="9",
        width=5,
        height=1
             ).grid(row=3, column=1)
zero = Button(
        root,
        text="0",
        width=5,
        height=1
             ).grid(row=3, column=2)

sin = Button(
        root,
        text="sin",
        width=5,
        height=1
             ).grid(row=3, column=3)

cos = Button(
        root,
        text="cos",
        width=5,
        height=1
             ).grid(row=3, column=4)

tan = Button(
        root,
        text="Tan",
        width=5,
        height=1
             ).grid(row=4, column=1)

sqrt = Button(
        root,
        text="Sqt",
        width=5,
        height=1
             ).grid(row=4, column=2)
reset = Button(
        root,
        text="C",
        width=5,
        height=1
             ).grid(row=4, column=3)
result = Button(
        root,
        text="=",
        width=5,
        height=1
             ).grid(row=4, column=4)

add = Button(
    root,
    text="+",
    width=5,
    height=1
).grid(row=1, column=0)

subtract = Button(
    root,
    text="-",
    width=5,
    height=1
).grid(row=2, column=0)

Multiply = Button(
    root,
    text="*",
    width=5,
    height=1
).grid(row=3, column=0)

Divide = Button(
    root,
    text="/",
    width=5,
    height=1
).grid(row=4, column=0)
root.mainloop()

I also tried to use frame for input field but got this error.

self.tk.call( _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

Code

from tkinter import *

root = Tk()
root.title("Calculator")
# input frame
input_frame = Frame(root)
input_frame.pack()
a = Entry(input_frame, width=100)
a.pack()
one = Button(
        root,
        text="1",
        width=5,
        height=1
             ).grid(row=1, column=1)

two = Button(
        root,
        text="2",
        width=5,
        height=1
             ).grid(row=1, column=2)

root.mainloop()

Is there any way to use both frames and grids to achieve this?

Upvotes: 0

Views: 661

Answers (1)

toyota Supra
toyota Supra

Reputation: 4560

Yes you can add Frame in every widgets. Change this:

Button(
        root,

To:

Button(
        input_frame,

This is what is look like:

enter image description here

Upvotes: 1

Related Questions