Python 3+ Tkinter Center Label Text
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 tkfrom 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 tkimport tkinter.ttk as ttkroot = 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 tkimport tkinter.ttk as ttkroot = 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()
1rst way:
from tkinter import *import tkinter.ttk as ttk
2nd way:
from tkinter import *from tkinter import ttk
With both ways, the first line imports the tk widgets and the second line imports the ttk widgets. The 1rst way is more clear but most tutorials uses the 2nd way.
Example:
root = Tk()button_1 = ttk.Button(root, text='Button 1').pack(padx=20, pady=10)button_2 = Button(root, text='Button 2').pack(padx=20, pady=10)root.mainloop()
So if you want to use the Button widgets from tkinter.ttk you write ttk.Button
(like button_1).If you want to use the Button widget from tkinter (the older one) you just call the Button
(like button_2).