Search code examples
pythonlayoutgridtkinterpack

Python Tkinter Layout Management with Grids


For a few days, I'm trying to set up a simple application with Tkinter and I have some problems to choose between pack, grid for my frames and widgets.

Here is a mockup of my app. I decided to delete the two buttons "Générer" so don't pay attention to that.

http://www.gilawhost.com/images/taajvonq.png

I have 3 frames : frameMatrix (for the checkboxes), frameImage (for the flashcode) and frameLink for the bottom part.

Those frames are implemented with grids. For exemple for the link part (bottom), I have something like this :

labelLink = LabelFrame(frameLink, text='Mini Lien', padx=5, pady=5)
labelMiniLien = Label(frameLink, text = "http://minilien.fr/").grid(row=0, column=0)
entryLink = Entry(frameLink, text=self.flashcode.lien).grid(row=0, column=1)
buttonLink = Button(frameLink, text="Suivre le lien").grid(row=0, column=2)

However, I don't know how to put my 3 frames together. My main frame is

self.frame=Frame(root)

And I tried to set my frames like below

frameMatrix=Frame(self.frame).grid(row=0, column=0)
frameImage=Frame(self.frame).grid(row=0, column=1)
frameLink=Frame(self.frame).grid(row=1, column=0, columnspan=2)

If you can help me, I tried several others things but nothing is correct With the code above, the frames overlap like if the grid of each frame was the same.

Thanks


Solution

  • The easiest IMO would be to use pack. Pack is perfect for this sort of layout, where you have frames that fill complete sides of an area. It's also better (and arguably easier) to separate widget creation from widget layout)

    For example:

    frameLine = Frame(...)
    frameMatrix = Frame(...)
    frameImage = Frame(...)
    ...
    frameLink.pack(side="bottom", fill="x")
    frameMatrix.pack(side="left", fill="both")
    frameImage.pack(side="right", fill="both")
    

    You'll probably want to add expand=True to one or more of those widgets, depending on what sort of resize behavior you desire.