Python_user
Python_user

Reputation: 1573

Frame object in tkinter not grouping widgets

I have just started to use the tkinter module. I am trying to group different widgets together into an entirely new widget using one of my custom classes which inherits from the Frame object and want to stack 4 such new widgets vertically.

Each of the new widgets contain 1 Label, 3 Entry widgets, and 2 Button widgets (with the button widgets serving different purposes in each one of the 4 new widgets).

To arrange them vertically, I am using the pack() method with no parameters. The problem is that instead of getting displayed in a vertical manner with all the 6 old widgets placed inside the new widget in a 'grouped' fashion, I am getting a 'skewed' display with all the buttons 'mixed' with each other (all the widgets with pack value of 'left' are displayed first while the widgets with pack value of 'right' are displayed next).

Here is part of my class OperationPanel (the actual class is too large to display but the other operations like subtraction etc. exist and all the functions to which the buttons are linked to also exist):

class OperationPanel(Frame):
    def __init__(self, app, operation):
        Frame.__init__(self, app)
        if operation == "addition":
            Label(app, text="Addition:").pack()
            self.num1 = Entry(app)
            self.num1.pack(side = LEFT)
            self.num2 = Entry(app)
            self.num2.pack(side = LEFT)
            self.disp = Entry(app)
            self.disp.pack(side = LEFT)
            Button(app, text="Reset", command=self.reset_add).pack(side = RIGHT)
            Button(app, text="Add!",  command=self.calc_add).pack(side = RIGHT)

        elif operation == "subtraction":
            Label(app, text="Subtraction:").pack()
            self.num1 = Entry(app)
            self.num1.pack(side = LEFT)
            self.num2 = Entry(app)
            self.num2.pack(side = LEFT)
            self.disp = Entry(app)
            self.disp.pack(side = LEFT)
            Button(app, text="Reset", command=self.reset_sub).pack(side = RIGHT)
            Button(app, text="Subtract!", command=self.calc_sub).pack(side = RIGHT)

        elif operation == "multiplication":
            Label(app, text="Multiplication:").pack()
            self.num1 = Entry(app)
            self.num1.pack(side = LEFT)
            self.num2 = Entry(app)
            self.num2.pack(side = LEFT)
            self.disp = Entry(app)
            self.disp.pack(side = LEFT)
            Button(app, text="Reset", command=self.reset_mul).pack(side = RIGHT)
            Button(app, text="Multiply!",  command=self.calc_mul).pack(side = RIGHT)

        elif operation == "division":
            Label(app, text="Division:").pack()
            self.num1 = Entry(app)
            self.num1.pack(side = LEFT)
            self.num2 = Entry(app)
            self.num2.pack(side = LEFT)
            self.disp = Entry(app)
            self.disp.pack(side = LEFT)
            Button(app, text="Reset", command=self.reset_div).pack(side = RIGHT)
            Button(app, text="Divide!",  command=self.calc_div).pack(side = RIGHT)
...

And here are my calls to pack the OperationPanel objects:

OperationPanel(app, "addition").pack()
OperationPanel(app, "subtraction").pack()
OperationPanel(app, "multiplication").pack()
OperationPanel(app, "division").pack()

And here is how the GUI looks right now:

Part 1:

the left side of the image

Part 2:

enter image description here

Upvotes: 0

Views: 397

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386305

It looks like you're creating all of the widgets with a parent of app. They parent should be self:

if operation == "addition":
    Label(app, text="Addition:").pack()
    self.num1 = Entry(self)
    ...

Upvotes: 2

Related Questions