Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

button with entry alignment with grid

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading