简体   繁体   中英

align widget with pack

I'm building a form with labels, fields and button. I want to learn how to align the start of the button with the start of the entry using the pack. I couldn't use side or anchor. Any suggestion?

# Cria o notebook
notebook = ttk.Notebook(root)
notebook.pack(side="top", anchor="w")
# Cria os frames
frm_save = ttk.Frame(notebook, padding=(0, 20))
frm_save.pack(side="left", fill="both", expand=True)
# frm_cadastrar.pack_propagate(False)
frm_praticar = ttk.Frame(notebook)
frm_praticar.pack(side="left", fill="both", expand=True)
# frm_praticar.pack_propagate(False)
# Adiciona os frames no notebook
notebook.add(frm_save, text="Cadastrar")
notebook.add(frm_praticar, text="Praticar")


# Formulário cadastrar
style.configure("TFrame")
frm1 = ttk.Frame(frm_save, style="TFrame")
frm1.pack(pady=10)

lb_english = ttk.Label(frm1, text="English", width=15, anchor="e", padding=(10, 0))
lb_english.pack(side="left")
entry_english = ttk.Entry(frm1)
entry_english.pack(side="left")

frm2 = ttk.Frame(frm_save)
frm2.pack(pady=10)

lb_portuguese = ttk.Label(frm2, text="Portuguese", width=15, anchor="e", padding=(10, 0))
lb_portuguese.pack(side="left")
entry_portuguese = ttk.Entry(frm2)
entry_portuguese.pack(side="left")


frm3 = ttk.Frame(frm_save)
frm3.pack()
bt_save = ttk.Button(frm3, text="Save")
bt_save.pack()

在此处输入图像描述

For this type of alignment, grid is the right tool for the job since you're clearly wanting to align widgets along a grid. If you insist on using pack , the best way would be to put two frames in the window: one on the left for the labels, and one on the right for the entry widgets and button. That should work fairly well for this simple example but doesn't scale well if you have a lot of widgets of different sizes.

The better solution is to use grid . Start by putting all of the labels, entries, and the button all in a single frame. You can then put the labels in column 0 aligned to the east, and the entry widgets and save button in column 1 aligned to the west.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM