Home > Software design >  Packing frame inside frame not as expected
Packing frame inside frame not as expected

Time:06-16

I'm trying to pack frame inside frame like this:

enter image description here

But my code give me this outcome:

enter image description here

How do I solve this? Here is the code:

import tkinter as tk
import pandas as pd

if __name__ == '__main__':
    top = tk.Tk()
    top.configure(bg='gray')
    top.geometry("700x350")
    
    table = tk.Frame(top, bg = "black")
    column1 = tk.Frame(table, height = 400, width = 100, bg = "pink", borderwidth=2)
    column2 = tk.Frame(table, height = 400, width = 100, bg = "yellow", borderwidth=2)
    col_1_row_1 = tk.Frame(column1, height = 100, width = 100, bg = "blue", borderwidth=2)
    col_1_row_2 = tk.Frame(column1, height = 100, width = 100, bg = "blue", borderwidth=2)
    col_1_row_3 = tk.Frame(column1, height = 100, width = 100, bg = "blue", borderwidth=2)
    col_1_row_4 = tk.Frame(column1, height = 100, width = 100, bg = "blue", borderwidth=2)

    table.pack(side=tk.TOP)
    column1.pack(side=tk.LEFT)
    column2.pack(side=tk.LEFT)
    
    col_1_row_1.pack(side=tk.TOP)

    top.mainloop()

CodePudding user response:

The vertical location of the column1 widget is not specified anywhere in the code. Simply replace the line column1.pack(side=tk.LEFT) with column1.pack(side=tk.LEFT,anchor='nw')

The anchor='nw' tells the layout manager to place this widget in the north-west (i.e. top left) corner of its parent frame.

  • Related