There's no trick -- the widget is centered in the area allocated to it by default. Simply place a label in a cell without any sticky attributes and it will be centered.
Now, the other question is, how to get the area it is allocated to be centered. That depends on many other factors, such as what other widgets are there, how they are arranged, etc.
Here's a simple example showing a single centered label. It does this by making sure the row and column it is in takes up all extra space. Notice that the label stays centered no matter how big you make the window.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This should be centered")
label.grid(row=1, column=1)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
if __name__ == "__main__":
root = tk.Tk()
Example(root).grid(sticky="nsew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.mainloop()
You can get a similar effect by giving a weight to all rows and columns except the one with the label.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This should be centered")
label.grid(row=1, column=1)
self.grid_rowconfigure(0, weight=1)
self.grid_rowconfigure(2, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(2, weight=1)
if __name__ == "__main__":
root = tk.Tk()
Example(root).grid(sticky="nsew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.mainloop()
Answer from Bryan Oakley on Stack OverflowThere's no trick -- the widget is centered in the area allocated to it by default. Simply place a label in a cell without any sticky attributes and it will be centered.
Now, the other question is, how to get the area it is allocated to be centered. That depends on many other factors, such as what other widgets are there, how they are arranged, etc.
Here's a simple example showing a single centered label. It does this by making sure the row and column it is in takes up all extra space. Notice that the label stays centered no matter how big you make the window.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This should be centered")
label.grid(row=1, column=1)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
if __name__ == "__main__":
root = tk.Tk()
Example(root).grid(sticky="nsew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.mainloop()
You can get a similar effect by giving a weight to all rows and columns except the one with the label.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This should be centered")
label.grid(row=1, column=1)
self.grid_rowconfigure(0, weight=1)
self.grid_rowconfigure(2, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(2, weight=1)
if __name__ == "__main__":
root = tk.Tk()
Example(root).grid(sticky="nsew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.mainloop()
There is nothing special required. A widget will be in the middle of it's parent automatically. What is required to to tell the parent to fill all available space.
from tkinter import *
root = Tk()
root.geometry("500x500+0+0")
frmMain = Frame(root,bg="blue")
startbutton = Button(frmMain, text="Start",height=1,width=4)
startbutton.grid()
#Configure the row/col of our frame and root window to be resizable and fill all available space
frmMain.grid(row=0, column=0, sticky="NESW")
frmMain.grid_rowconfigure(0, weight=1)
frmMain.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.mainloop()
This uses grid rather than pack to place the widgets and the grid is configured to fill the entire size of the window. The button will appear in the centre regardless of the size of the window.
How can I center a label using Tkinter's grid method in Python - Stack Overflow
python - Tkinter: Center label in frame of fixed size? - Stack Overflow
Tkinter GUI Centering?
python - How to center a label in a tkinter colspan using grid? - Stack Overflow
Videos
[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")
grid by default will center a widget in the space allocated to it. The problem is that you've not configured the column to grow to fill the window. Also by default, a row or column will be just large enough to fit the widgets in it.
To do that, you need to give column 0 a non-zero weight:
mw.grid_columnconfigure(0, weight=1)
Do you need to use the grid() method for your layout? pack() would automatically center it.
from tkinter import *
from tkinter import ttk
mw=Tk()
mw.title('Window')
mw_width, mw_height = 300, 200
mw.minsize(mw_width,mw_height)
frame1=Frame(mw)
frame1.pack()
main_label=ttk.Label(frame1,text='My label', justify="center")
main_label.pack()
mw.mainloop()
You could also use place().
from tkinter import *
from tkinter import ttk
mw = Tk()
mw.title('Window')
mw_width, mw_height = 300, 200
mw.minsize(mw_width, mw_height)
frame1 = Frame(mw)
frame1.place(width=mw_width, height=mw_height)
main_label = ttk.Label(frame1, text='My label', justify="center")
main_label.place(relx=.5, rely=0, anchor="n")
mw.mainloop()
I’ve coded a wordle GUI using Tkinter. I won’t include all the code as it’s ~600 lines, but the main problem revolves around the following code (sorry, idk how to make it an actual code box in Reddit):
widget = Label(root, text = ‘ ‘, font = (‘Ariel’, 25)) widget.grid(column = 4, row = 4)
This will make my label, set the parameters, and then align it with the GUI grid.
It all works great except for one thing - the text box displaying “The word was “_____”!” pushes all the other widgets to the side. I KNOW this is because I’m using button.grid() to set the locations of all of the buttons, and when the text is finally updated in that text box (changes from ‘ ‘ to the above text), it makes the width much larger than the other center widgets.
Is there a way within Tkinter to get it to “ignore” the other widgets so it can be whatever width it wants to be without pushing the other widgets to the edges?
You put the label in column 1 and told it to span two columns. Therefore it is in column 1 and column 2.
I assume you want it to span all three columns, so the solution is to move the label to column zero and have it span three columns:
b.grid(row=0, column=0, columnspan=3)
You could easily solve with this
lbl_address = ttk.Label(addmission_form,text="Address")
lbl_address.grid(row=7,column=0,padx=10,pady=10)
txtBox_address = ttk.Entry(addmission_form ,width=100)
txtBox_address.grid(row=7,column=1,columnspan=3,padx=40,pady=10)
the out of this code will be this enter image description here
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()
