Python Tkinter Grid (grid() method in Python Tkinter)

Want to know how to use the Python Tkinter grid? In this tutorial, we are covering everything on Grid in Python Tkinter. We will see how to use grid() method in Python Tkinter. Also, we will cover these topics:

  • Python Tkinter Grid
  • How to use Python Tkinter grid() method
  • Python Tkinter Grid Example
  • Python Tkinter Grid Align Left
  • Python Tkinter Grid Function
  • Python Tkinter Grid Padding
  • Python Tkinter Grid Columnspan
  • Python Tkinter Grid Spacing
  • Python Tkinter Grid Not Working
  • Python Tkinter Grid Expand
  • Python Tkinter Grid Fill

Python Tkinter Grid

  • Python Tkinter provides widgets that make the application appear good and easy to use for the users. Placement of widgets on the application is equally important as choosing the right widget.
  • Python Tkinter provides three types of Layout managers that help the developer to place the widget at the right spot on the application.
    • Pack: it packs all the widgets in the center and places widgets in a sequence.
    • Grid: Grid places widgets in a row and column-wise.
    • Place: Place positions widgets in X and Y coordinates.
  • In this tutorial, we will discuss the Python Tkinter Grid() method. We will explore all the options available in the grid layout manager in Python Tkinter.

Python Tkinter Grid Example

In this section, we will see an example for Grid Layout manager in Python Tkinter also we’ll see few features of grid layout manager in Python Tkinter.

  • Grid Layout manager requires rows and columns as an argument using this information it places the widget on the screen.
  • rows and columns start from the top left of the screen. You can visualize the spreadsheet to understand it better.
  • columnspan is used to occupy the other cells of columns like in the below example you can see that button is occupying two cells’ space.
  • Here is an example of implementing a grid on the widgets. In this example, we have used label, entry, and button widgets.
grid() method in Python Tkinter
Python Tkinter Grid

Source code of the above example:

In this example, we have placed the label widgets in the column 0, entry widgets at column 1. Whereas button is expanded to two columns.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#9FD996')

Label(
    ws,
    text='Enter Email', 
    bg='#9FD996'
).grid(row=0, column=0)

Label(
    ws,
    text='Enter Password',
    bg='#9FD996'
).grid(row=1, column=0)

Entry(ws).grid(row=0, column=1)
Entry(ws).grid(row=1, column=1)

Button(
    ws,
    text='Login',
    command=None
).grid(row=2, columnspan=2)


ws.mainloop()

Python Tkinter Grid Align Left

  • The grid layout manager in Python Tkinter provides a sticky option that allows aligning the widgets to the LEFT, RIGHT, TOP, BOTTOM direction. In this section, we will see how to set alignment to Left using Grid in Python Tkinter.
  • Sticky requires the first letter of the universal direction in lower case to set the widget in the direction of choice. Here are the options
    • sticky = ‘e’ : Align to Right
    • sticky = ‘w’: Align to Left
    • sticky = ‘n’: Align to Top
    • sticky = ‘s’: Align to bottom
grid(row=0, column=1, sticky='w')

Example of Python Tkinter Grid Align to Left:

In this example, we have displayed two images. In the first image Label widget and Button widget are not aligned whereas in the second image we have aligned both the widget to left using sticky option in Grid layout manager of Python Tkinter.

Python Tkinter Grid
Python Tkinter Grid align left using sticky

Python Tkinter Grid Function

  • Grid in Python Tkinter is a function that allows managing the layout of the widget. It takes rows and columns as an argument and places the widget in 2D format.
  • Due to its 2D property, Python Tkinter Grid is widely used for games and applications like Calculator, Tic-Tac-Toe, Mines, etc.
  • Apart from rows and columns, Grid offers a few more options:
OptionsDescription
column, rowThe column refers to the verticals cells of the table
row refers to the horizontal cells of the table
columnspan, rowspancolumnspan merge the columns to provide more space
rowspan merge the rows to provide more space horizontally
ipadx, ipadyipadx add pixels vertically in the widget’s border
ipady add pixels horizontally in the widget’s border
padx, padypadx add pixels vertically outside the widget’s border
pady add pixels horizontally outside the widget’s border
stickyit sticks the widget in North, South, East, west directions.

Python Tkinter Grid Padding

  • Grid in Python Tkinter provides a padding option using which space can be added inside as well as outside the widget’s border.
  • While organizing a widget at times the default size or position of the widget didn’t meet the developer’s expectation. At that time using padding, a developer adjusts the widget.
  • There are two types of padding:
    • Padding inside the widget’s border
    • Padding outside the widget’s border
  • ipadx, ipady are used to add spaces horizontally and vertically inside the widget’s border.
  • padx, pady are used to add spaces horizontally and vertically outside the widget’s border.

Example of Grid padding in Python Tkinter

In this example, four pictures are displayed and each shows the change in button when padding is added. We have choose button 5 which is in the center of all the buttons.

Image 1: image one don’t have any padding. The purpose of adding this image is to show you the original image so that you can compare it with the changed images.

Image 2: We have added ipadx=10 and ipady=10, with this 10 pixels are added inside the widget’s border and it has started looking bigger.

Image 3: We have added padx=10 and pady=10, with this 10 pixels are added outside the widget’s border and as a result of which other widgets have to move away.

Image 4: Fourth image is the final image and the source code of same is provided below. In this we have applied both the padding, as a result of which button 5 is looking bigger and other widgets are shifted by 10 pixel.

python tkinter grid padding
Python Tkinter Grid

Code Snippet for the above diagram

Here is the code snippet for the above-mentioned program. In this code, we have provided both inside and outside widgets width. So this code will produce the output on the fourth picture. The exact code is available on line 15.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#F2B33D')

frame = Frame(ws, bg='#F2B33D')

Button(frame, text="7").grid(row=0, column=0, sticky='ew')
Button(frame, text="8").grid(row=0, column=1)
Button(frame, text="9").grid(row=0, column=2)

Button(frame, text="4 ").grid(row=1, column=0)
Button(frame, text="5").grid(row=1, column=1, ipadx=10, ipady=10, padx=10, pady=10)
Button(frame, text="6").grid(row=1, column=2)

Button(frame, text="7 ").grid(row=2, column=0)
Button(frame, text="8").grid(row=2, column=1)
Button(frame, text="9").grid(row=2, column=2)

frame.pack(expand=True) 

ws.mainloop()

Python Tkinter Grid Columnspan

  • Grid in Python Tkinter provides columnspan using which we can merge two or more cells into one.
  • columnspan is helpful in cases when you need extra space for a particular widget.
  • columnspan accepts an integer as an argument and merges the same number of cells together.
Python Tkinter Grid Columnspan
Python Tkinter Grid Columnspan

Code Snippet:

In this code, online 14 we have use columnspan=3, it has merged three cells into one and displayed button to it. The button is expanded to the available space because we have set sticky=’ew’.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#F2B33D')

frame = Frame(ws, bg='#F2B33D')

Button(frame, text="These buttons").grid(row=0, column=0)
Button(frame, text="are positioned").grid(row=0, column=1)
Button(frame, text="using Grid").grid(row=0, column=2)

Button(frame, text="another line").grid(row=1, columnspan=3, sticky='ew')


frame.pack(expand=True) 

ws.mainloop()

Python Tkinter Grid Spacing

  • Grid in Python Tkinter provides a padding option using which space can be added inside as well as outside the widget’s border.
  • While organizing a widget at times the default size or position of the widget didn’t meet the developer’s expectation.
  • At that time using padding, a developer adjusts the widget. There are two types of padding:
    • Padding inside the widget’s border
    • Padding outside the widget’s border
  • ipadx, ipady are used to add spaces horizontally and vertically inside the widget’s border. padx, pady are used to add spaces horizontally and vertically outside the widget’s border.
  • Refer to Python Tkinter Grid Padding to see an example.

Python Tkinter Grid Not Working

In this section, we will discuss common errors or mistakes due to which Python Tkinter Grid doesn’t work.

  • Columnspan’s value can’t be greater than total number of columns. If you have 5 columns in the application then maximum number of columns could be merge is 5. In case you have entered more than 5 then it will merge till 5 only

Python Tkinter Grid Expand

  • Grid in Python Tkinter provides a sticky using which a widget can be expanded in X & Y directions. Other Layout managers like Place and Pack have a expand option using which widget can be allowed to expand expand =True.
  • Sticky requires the first letter of the universal direction in lower case to set the widget in the direction of choice. Here are the options
    • sticky = ‘e’ : Align to Right
    • sticky = ‘w’: Align to Left sticky = ‘n’: Align to Top sticky = ‘s’: Align to bottom

By providing a combination of directions we can the widget in the available space. For example, if you want to fill the X position of the widget then type: sticky = 'ew'. here, ‘e’ will cover the east direction of the widget and ‘w‘ will cover the west direction.

Python Tkinter Grid Fill

  • Grid in Python Tkinter provides a sticky using which a widget can be expanded in X & Y directions. Other Layout managers like Place and Pack have a fill option using which widget can be allowed to expand in available space if expand =True.
  • Sticky requires the first letter of the universal direction in lower case to set the widget in the direction of choice. Here are the options
    • sticky = ‘e’ : Align to Right
    • sticky = ‘w’: Align to Left
    • sticky = ‘n’: Align to Top
    • sticky = ‘s’: Align to bottom
  • By providing a combination of directions we can the widget in the available space. For example, if you want to fill the X position of the widget then type: sticky = 'ew'. here, ‘e’ will cover the east direction of the widget and ‘w‘ will cover the west direction.

Example of Grid Fill in Python Tkinter

In this code, we have implemented grid fill in Python Tkinter. Grid always starts from the top-left corner of the screen so we have created a frame, placed that frame at the center then we have created buttons with the grid layout manager. On lines 11, 12 we have used sticky to stretch the button in the left-right direction.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#F2B33D')

frame = Frame(ws, bg='#F2B33D')

Button(frame, text="These buttons").grid(row=0, column=0)
Button(frame, text="are positioned").grid(row=0, column=1)
Button(frame, text="using Grid").grid(row=0, column=2)

Button(frame, text="This is ").grid(row=1, column=0, sticky='ew')
Button(frame, text="another line").grid(row=1, column=1, sticky='ew')
Button(frame, text="using Grid").grid(row=1, column=2)

frame.pack(expand=True) 

ws.mainloop()

Output:

In this output, buttons are displayed on the screen using a grid in Python Tkinter. Tow buttons are small because of less number of words. So to fill that ugly space we have used sticky=’ew’ on those two buttons. Now you can see in the right picture that all the buttons are organized and there is not empty space.

python tkinter grid fill
Python Tkinter Grid Fill

In this tutorial, we have learned everything about Gird Layout Manager in Python Tkinter. Also, we have covered these topics.

  • Python Tkinter
  • How to use grid() method in Python Tkinter
  • Python Tkinter Grid Layout Example
  • Python Tkinter Grid Align Left
  • Python Tkinter Grid Function
  • Python Tkinter Grid Padding
  • Python Tkinter Grid Columnspan
  • Python Tkinter Grid Spacing
  • Python Tkinter Grid Not Working
  • Python Tkinter Grid Expand
  • Python Tkinter Grid Fill