Search code examples
pythonuser-interfacetkinterframe

How to align a frame to the left in tkinter?


So I created this GUI in Tkinter. The X, Y and Z labels and entries are in a frame, and I need that frame to be justified to the left (I want it to be next to the "Nombre" entry). I tried the sticky = "w" option in the frame but that didn't seem to work. Thank you for the help in advance!

enter image description here


Solution

  • There are several ways to do it. The most logical choice is to use grid, or a combination of grid and pack. I think the combination makes the most sense -- a frame with the x, y, and z widgets can use pack, and then that frame and the other widgets can be laid out using grid.

    Something like this, perhaps:

    frame.grid_columnconfigure(3, weight=1)
    
    x_button.grid(row=0, column=0, rowspan=2, sticky="nsew", padx=2, pady=2)
    nombre_label.grid(row=0, column=1, sticky="w")
    nombre_entry.grid(row=0, column=2, sticky="ew")
    xyz_frame.grid(row=0, column=3, sticky="w")
    descripción_label.grid(row=1, column=1)
    descripción_entry.grid(row=1, column=2, columnspan=2, sticky="ew")
    
    x_label.pack(side="left", in_=xyz_frame)
    x_entry.pack(side="left", in_=xyz_frame)
    y_label.pack(side="left", in_=xyz_frame)
    y_entry.pack(side="left", in_=xyz_frame)
    z_label.pack(side="left", in_=xyz_frame)
    z_entry.pack(side="left", in_=xyz_frame)
    

    screenshot