You can use .place() for your label since your frame and your label have different parents. In place() you can use anchor="center" specify the startingpoint of your "anchor" with: x and y. Here is a working example:
app = Tk()
f = Frame(app,bg="yellow",width=50,height=50)
f.grid(row=0,column=0,sticky="NW")
f.grid_propagate(0)
f.update()
l = Label(f,text="123",bg="yellow")
l.place(x=25, y=25, anchor="center")
app.mainloop()
Answer from VRage on Stack OverflowTkinter: How can I align the text inside a label widget?
How to center a Label in a Full Screen Root (tkinter, python 3)
In a line like
LabelTitle.place(x=x, y=y, anchor="center")
the anchor means to place the center of the label and the coordinates given by x and y.
Anyway, the thing is that Tk doesn't know how big the window is going to be until the main loop runs, so unless there's a flag to tell it to put the label there at that time (and there may be, I don't use Tk very often), you can't just set the position beforehand. I would do something like this:
def position_label(*args):
LabelTitle.place(x=root.winfo_width() // 2, y=root.winfo_height() // 2, anchor='center')
root.bind("<FocusIn>", position_label)
So the root window gets focus after its geometry has been asserted, so we hook in a callback (position_label) to position the label at that time.
[tkinter] How do I vertically center text to window in GUI?
python - Tkinter center Label in the GUI - Stack Overflow
Videos
The problem is that you are reading the documentation for the tkinter label but you are using a ttk label. This is why you should not use wildcard imports -- when two modules export objects with the same name (eg: tkinter.Label and ttk.Label) it becomes difficult to know which one is being used in your code. The default for a ttk label is to be aligned left, but the tkinter label is aligned center, and the order of your imports means that you're using a ttk Label.
The quick fix for your example is to explicitly set the anchor option for the ttk label (eg: label.configure(anchor="center")).
You should also fix your imports so that this problem doesn't happen to you again. Instead of doing a wildcard import (eg: from tkinter import *) you should explicitly import the module as a unit, optionally with a shorter name. Once you do that, you need to prefix your widgets with the name of the module.
For example, given these import statements:
import tkinter as tk
from tkinter import ttk
... you would then create a ttk label with ttk.Label(...), and a tkinter label with tk.Label(...) which makes your code much easier to understand, and it removes all ambiguity.
This is a textbook example of namespace clustering. You're clustering python's namespace, with the lines:
from tkinter import *
from tkinter.ttk import *
this means if there's a tkinter.ttk class that has the same name that of tkinter class, ttk one will be used, such as Button and Label. And apparently ttk not necessarily have the tkinter.Label's justify option. A simple position swap is sufficed to demonstrate the difference swap the imports to:
from tkinter.ttk import *
from tkinter import *
Instead and see what happens.
See below example with center justified text where no namespaces are clustered, using tkinter.Label as label:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
label = tk.Label(root, text="Test Callback")
btn = tk.Button(root, text="Text so long that root has to resize.")
btn.pack()
label.pack(fill='both', expand=True)
root.mainloop()
See below example center justified text where no namespaces are clustered, using tkinter.ttk.Label as label:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
label = ttk.Label(root, text="Test Callback")
btn = tk.Button(root, text="Text so long that root has to resize.")
btn.pack()
label.pack(expand=True)
root.mainloop()
[Solved]
If I create a simple label width a text, the widget content is always centered. How can I make the content of the widget align left? (Not the widget itself, but the text in the widget)Wherever I search, I always see the same answer: Set the anchor or sticky option as 'w'. But that still keeps the content of the widget centered.These examples (pack and grid) both still center the text inside the widget:
label = tk.Label(root, text="Line 1\nSecond line", anchor="w") label.pack() label = tk.Label(root, text="Line 1\nSecond line") label.grid(row=0, column=0, sticky="w")
I am trying to do a game-app main screen using tkinter. I google some sintax in order to make a fullscreen root and frame, but i couldnt find how to center the game title... i´ve tried to se Label.place(anchor="center") but it just insert the lebel on the left top of the screen... if you wanna see the code here it is:
import random
from tkinter import *
import tkinter.font
root=Tk()
root.config(bg="#521D46")
root.attributes('-fullscreen', True)
root.bind('<Escape>',lambda e: root.destroy())
Frame1=Frame(root)
Frame1.pack(fill="both", expand=True, padx=2, pady=2)
Frame1.config(bg="#792566",bd=10,relief="flat")
LabelTitle=Label(Frame1, text="SEQUENCE GAME", bg="#521D46", font="TkFixedFont
24",fg="white")
LabelTitle.place(anchor="center")
root.mainloop()I dont know what else to do :( ...I´d really appreciate if you help me!
In my opinion, you should have used place or grid instead of pack. Because pack only gives few alignment options.
otherwise, maybe divide the main window into two frames then pack the label at the top of the lower frame
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
L = Label(root, text="Welcome to 3P Connect 4!!!",font=("Ariel",20,"bold", "underline"))
L.pack(side = TOP)
I wish this helps. but you should use grid for better alignment or place.
You can use the following commands to place the label in the center
L = Label(root, text="Welcome to 3P Connect 4!!!",font=("Ariel",20,"bold", "underline"))
# L.config(anchor=CENTER)
# L.pack()
L.place(x=HEIGHT/2, y=WIDTH/2, anchor="center")
Similarly, you can also use button.place(x=100, y=25) for buttons
REF: Tkinter: Center label in frame of fixed size?
In the case of placing a single widget in the center of another widget, place is the best choice. It has options to locate a widget relative to another widget (usually, but not necessarily, relative to its parent).
In your case, you want the center of the label in the center of its parent. You can use a relative X coordinate of .5, a relative Y coordinate of .5, and an anchor of "center", meaning that the center of the widget is placed at the given coordinates.
Example:
nameLabel.place(relx=.5, rely=.5, anchor="center")
Tkinter has two options to show widgets
grid() or pack()
Using pack():
In this case this is probably the best option -since you are dealing with only 1 widget:
By default top and bottom will align a widget to the center. Left and right will align to the left and right respectively.
Here an extract of your code demonstrating this property:
nameLabel = Label(welcome, text="Welcome", bg="gray", fg="white")
nameLabel.pack(side="top")
You can also center align text at the bottom as well:
nameLabel = Label(welcome, text="Welcome", bg="gray", fg="white")
nameLabel.pack(side="bottom")
Using grid():
Sometimes it may become necessary to align multiple widgets. You can position them individually and accurately using grid():
nameLabel1 = Label(welcome, text="Welcome", bg="gray", fg="white")
nameLabel1.grid(column=0, row = 1)
nameLabel2 = Label(welcome, text="Welcome", bg="gray", fg="white")
nameLable2.grid(column=1, row = 1)
nameLabel3 = Label(welcome, text="Welcome", bg="gray", fg="white")
nameLabel3.grid(column=2, row = 1)
Update: Further to your centring label in window comment I do not know how to do this myself although I have found this post that seems to do what you want