How do I make my labels in tkinter centralize?

Basically the question. I have my layout of my top row of the GUI exactly as I want it apart from the fact that I need the title to be in the center with gameturns to the left and gamerounds to the right. I searched the answer to the question myself and only found answers that basically said “.grid does it by default” which I didn’t really get. I’ve tried doing sticky=”nsew” on my .grid configuration, but they still won’t go to the center. Am I misunderstanding how sticky works? How do I make everything on my first row be in the center?

#game GUI goes here

maingame = tk.Tk()
maingame.title("Louise's Card Game")
maingame.geometry("600x350")
maingame.resizable(0, 0)

#top row labels
gameturns = tk.Label(maingame, text="{}'s Turn".format(currentTurn), font=("Courier", 10), padx=10, pady=10)
gametitle = tk.Label(maingame, text="Louise's Card Game", font=("Courier", 15), padx=10, pady=10)
gamerounds = tk.Label(maingame, text=("Round {}".format(currentRound)), font=("Courier", 10), padx=10, pady=10)

#top row positionings
gameturns.grid(row=0, column=0)
gametitle.grid(row=0, column=1)
gamerounds.grid(row=0, column=2)

    
maingame.mainloop()

Answer

You can put the three labels in a frame and use .pack() on the frame:

...
frame = tk.Frame(maingame)
frame.pack()

# put top row labels in the frame
gameturns = tk.Label(frame, text="{}'s Turn".format(currentTurn), font=("Courier", 10), padx=10, pady=10)
gametitle = tk.Label(frame, text="Louise's Card Game", font=("Courier", 15), padx=10, pady=10)
gamerounds = tk.Label(frame, text=("Round {}".format(currentRound)), font=("Courier", 10), padx=10, pady=10)

#top row positionings
gameturns.grid(row=0, column=0)
gametitle.grid(row=0, column=1)
gamerounds.grid(row=0, column=2)

Then the three labels will be put in the top center of the window.

Note that if grid() is used in other widgets inside maingame window, you can use back grid() on the frame.


If you still want to put the three labels inside maingame using grid(), try:

maingame.columnconfigure((0,2), weight=1, uniform=1)

#top row labels
gameturns = tk.Label(maingame, text="{}'s Turn".format(currentTurn), font=("Courier", 10), padx=10, pady=10)
gametitle = tk.Label(maingame, text="Louise's Card Game", font=("Courier", 15), padx=10, pady=10)
gamerounds = tk.Label(maingame, text=("Round {}".format(currentRound)), font=("Courier", 10), padx=10, pady=10)

#top row positionings
gameturns.grid(row=0, column=0, sticky='e')
gametitle.grid(row=0, column=1)
gamerounds.grid(row=0, column=2, sticky='w')