Frederick Reynolds
Frederick Reynolds

Reputation: 666

Using self.pack and self.grid works without classes, but fails with them

So I tried to use Tkinter Button Alignment in Grid to make a toolbar, but the difference is im using classes. So while they use frame.pack and button.grid, i get the error of using both pack and grid. Here is my code:

class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.master.title('Hi!')
        self.master.configure(bg='dark grey')
        self.pack(fill=X, side=TOP)
        self.create_toolbar()

    def create_toolbar(self):
        Home = Button(text='Home', bd=1)
        About = Button(text='About', bd=1)

        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)

        Home.grid(row=0, column=0, sticky=W+E)
        About.grid(row=0, column=1, sticky=W+E)

Upvotes: 0

Views: 211

Answers (1)

cmacboyd
cmacboyd

Reputation: 51

It seems to me that the issue here is that the Widgets don't have master set, so they are defaulting to the class' master, where you have already used pack for geometry management.

The solution here is to set master=self when you are declaring your widgets, then when you go to place the widgets in the tk.Frame object you can use grid.

Upvotes: 1

Related Questions