Home > Blockchain >  How to create left, right, and center frames with pack?
How to create left, right, and center frames with pack?

Time:10-30

I have a left and right frame right now and tried creating a center frame but the problem is the left frame takes up more space and pushes the center frame to the right so any widgets I put in it aren't actually centered. Is there any way to make it work?

    self.leftside = ttk.Frame(self)
    self.leftside.pack(expand=True, fill=BOTH, side=LEFT, anchor=W)

    self.center = ttk.Frame(self)
    self.center.pack(expand=True, fill=BOTH, side=LEFT, anchor=CENTER)

    self.rightside = ttk.Frame(self)
    self.rightside.pack(expand=True, fill=BOTH, side=RIGHT, anchor=E)

CodePudding user response:

If you are wanting to guarantee that the center section says centered, grid is going to be a better choice than pack since you can configure grid to force the other two columns to be the same size, and to have the center region grow or shrink to fill the rest of the space.

It would look something like this:

    self.leftside.grid(row=0, column=0, sticky="nsew")
    self.rightside.grid(row=0, column=2, sticky="nsew")
    self.center.grid(row=0, column=1, sticky="nsew")

    self.grid_rowconfigure(0, weight=1)
    self.grid_columnconfigure((0,2), uniform="equal")
    self.grid_columnconfigure(1, weight=1)
  • Related