Home > Software design >  button with entry alignment with grid
button with entry alignment with grid

Time:09-07

I'm learning tkinter and I'm having trouble using the pack and grid geometry manager. Using the grid I am not able to align two buttons with the entry, as shown in the image. Any instructions in this direction?

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


lb_english = ttk.Label(frm_principal, text="English", width=15, anchor="e", padding=(10, 0))
lb_english.grid(row=0, column=0, pady=5)
entry_english = ttk.Entry(frm_principal)
entry_english.grid(row=0, column=1, pady=5)


lb_portuguese = ttk.Label(frm_principal, text="Portuguese", width=15, anchor="e", padding=(10, 0))
lb_portuguese.grid(row=1, column=0, pady=5)
entry_portuguese = ttk.Entry(frm_principal)
entry_portuguese.grid(row=1, column=1, pady=5)

frm2 = ttk.Frame(frm_save)
frm2.pack()
bt_save = ttk.Button(frm2, text="Save", command=cadastrar)
bt_save.grid(row=2, column=0, sticky="w", )
bt_cancelar = ttk.Button(frm2, text="Cancelar", command=limpar_entry)
bt_cancelar.grid(row=2, column=1, sticky="e")

enter image description here

CodePudding user response:

The simple way is to create frm2 as a child of frm_principal and put it at row=2 and column=1.

Below is the modified code:

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


lb_english = ttk.Label(frm_principal, text="English", width=15, anchor="e", padding=(10, 0))
lb_english.grid(row=0, column=0, pady=5)
entry_english = ttk.Entry(frm_principal)
entry_english.grid(row=0, column=1, pady=5, sticky="w") # added sticky="w"


lb_portuguese = ttk.Label(frm_principal, text="Portuguese", width=15, anchor="e", padding=(10, 0))
lb_portuguese.grid(row=1, column=0, pady=5)
entry_portuguese = ttk.Entry(frm_principal)
entry_portuguese.grid(row=1, column=1, pady=5, sticky="w") # added sticky="w"

frm2 = ttk.Frame(frm_principal) # use frm_principal as parent
frm2.grid(row=2, column=1)      # use grid() instead of pack()
bt_save = ttk.Button(frm2, text="Save", command=cadastrar)
bt_save.grid(row=2, column=0, sticky="w")
bt_cancelar = ttk.Button(frm2, text="Cancelar", command=limpar_entry)
bt_cancelar.grid(row=2, column=1, sticky="e")
  • Related