I want to centralize frm_login using pack or grid without using another auxiliary widget. Is it possible?
I put an anchor="center" in frm_login.pack(side="left") but it didn't work.
import tkinter as tk
from tkinter import ttk
principal = tk.Tk()
principal.title("Login")
principal.resizable(False, False)
largura = 300
altura = 200
posx = int(principal.winfo_screenwidth() / 2 - largura / 2)
posy = int(principal.winfo_screenheight() / 2 - altura / 1.2)
principal.geometry("{0}x{1}+{2}+{3}".format(largura, altura, posx, posy))
frm_login = ttk.Frame(principal)
frm_login.pack(side="left")
lb_usuario = ttk.Label(frm_login, text="Usuário")
lb_usuario.grid(row=0, column=0, padx=5, pady=5, sticky="e")
ed_usuario = ttk.Entry(frm_login, width=24)
ed_usuario.grid(row=0, column=1, sticky="w")
lb_senha = ttk.Label(frm_login, text="Senha")
lb_senha.grid(row=1, column=0, padx=5, pady=5, sticky="e")
ed_senha = ttk.Entry(frm_login, width=24)
ed_senha.grid(row=1, column=1, sticky="w")
frm_botoes = ttk.Frame(frm_login)
frm_botoes.grid(row=2, column=1, pady=5, sticky="w")
bt_entrar = ttk.Button(frm_botoes, text="Entrar")
bt_entrar.grid(row=1, column=1)
bt_sair = ttk.Button(frm_botoes, text="Sair")
bt_sair.grid(row=1, column=2)
principal.mainloop()

If you add expand=True to the call to pack, it will be centered. expand tells the packer to expand the allocated space to consume all extra space. By default the frame will be centered in the space allocated. The net result is that the frame will be centered in the window.
frm_login.pack(side="left", expand=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With