Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to horizontally center a widget using grid()?

I am using grid() to place widgets in a tkinter window. I am trying to put a label on the horizontal center of a window and have it stay there, even if the window is resized. How could I go about doing this?

I don't want to use pack(), by the way. I would like to keep using grid().

like image 416
L4undry Avatar asked Oct 09 '15 20:10

L4undry


People also ask

How do you center a grid label?

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.

How do you center something in Tkinter?

To configure and align the text at the CENTER of a Tkinter Text widget, we can use justify=CENTER property.

What is pack () in Tkinter?

pack is the easiest Layout Manager to code with in Tkinter. Instead of declaring the precise location of a widget, the pack() method declares the position of widgets in relation to each other.

How do you use PADX and Pady in Tkinter?

padx, pady − How many pixels to pad widget, horizontally and vertically, outside v's borders. row − The row to put widget in; default the first row that is still empty. rowspan − How many rowswidget occupies; default 1. sticky − What to do if the cell is larger than widget.


1 Answers

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()
like image 178
Bryan Oakley Avatar answered Oct 26 '22 05:10

Bryan Oakley