Updated March 31, 2023
Introduction to Tkinter Table
Tkinter Table is used to display data in the form of rows and columns. Tkinter does not provide any Table widget to create the table, but we have other alternative methods. Python GUI-tkinter has multiple options for developing Graphical User Interfaces. Tkinter module is a standard interface to the tk GUI combined with Python. Python tkinter has 3 geometry manager classes out of which grid() method organizes the widget in a table-like structure before placing into parent widget.
This grid() method works by splitting a Frame or window into rows and columns, location of the widget can be specified by calling grid() method with passing indices of rows to keyword arguments, respectively.
Syntax:
grid_name = tk.Frame(
master=window,
relief=tk.<type_of_border>,
borderwidth=2
)
grid_name.grid(row=<row_element>, column=<column_element>)
Possible Grid options are listed below:
- column_element: variable of the column element
- row_element: variable of the row element
- borderwidth: user has to define the width of the border
- relief: Type of border is specified.
Tkinter provides classes that allow the display of widgets. positioning and control of widgets. Some of the top-level widgets are Tk and TopLevel. Also other widgets like Label, Entry, Text, Frame, Canvas, RadioButton, Button, CheckButton, Listbox, Scrollbar, Scale, Spinbox, PanedWindow, and LabelFrame.
Tkinter gives an Object-Oriented Interface to tk GUI toolkit.
How Tkinter in Python GUI works?
- Importing of Tkinter Module
- Creating GUI application main window
- Adding one or more widgets to the application
- Entering main event loop to take action on each of the event triggered by the user
Examples of Tkinter Table
Lets us discuss examples of Tkinter Table.
Example #1: Simple Grid Tkinter Table
Code:
import tkinter as tkinter
window = tkinter.Tk()
for x in range(2):
for y in range(3):
frameGrid = tkinter.Frame(
master=window,
relief=tkinter.FLAT,
borderwidth=2
)
frameGrid.grid(row=x, column=y)
labelGrid = tkinter.Label(master=frameGrid, text=f"Row No. {x}\nColumn No. {y}")
labelGrid.pack()
window.mainloop()
Output:
Example #2: Tkinter Table with Different Type of Border
Code:
import tkinter as tkinter
window = tkinter.Tk()
for x in range(5):
for y in range(4):
frameGrid = tkinter.Frame(
master=window,
relief=tkinter.RAISED,
borderwidth=2
)
frameGrid.grid(row=x, column=y)
labelGrid = tkinter.Label(master=frameGrid, text=f"Row No. {x}\nColumn No. {y}")
labelGrid.pack()
window.mainloop()
Output:
Example #3: Tkinter Table grid() manager with padding
Code:
import tkinter as tkinter
window = tkinter.Tk()
for x in range(4):
for y in range(2):
framegrid = tkinter.Frame(
master=window,
relief=tkinter.SUNKEN,
borderwidth=1.5
)
framegrid.grid(row=x, column=y, padx=5, pady=5)
labelgrid = tkinter.Label(master=framegrid, text=f"Row no. {x}\nColumn no. {y}")
labelgrid.pack(padx=3, pady=3)
window.mainloop()
Output:
Example #4: Python Tkinter Table with padx and pady and RIDGE relief
Code:
import tkinter as tkinter
window = tkinter.Tk()
for x in range(5):
window.columnconfigure(x, weight=2, minsize=25)
window.rowconfigure(x, weight=1, minsize=45)
for y in range(0,6):
framegrid = tkinter.Frame(
master=window,
relief=tkinter.RIDGE,
borderwidth=1.5
)
framegrid.grid(row=x, column=y, padx=7, pady=3)
labelgrid = tkinter.Label(master=framegrid, text=f"Row no. {x}\nColumn no. {y}")
labelgrid.pack()
window.mainloop()
Output:
On setting the columnconfigure() and rowconfigure(), rows and column adjust according to increase or decrease in window size. Keyword weight determines how row or column should respond based on window resizing.
Keyword minsize helps in setting minsize of row height and column width.
.grid() splits a window of the console or the Frame into rows and columns. Location of the grid() widget for creating a tkinter table is specified by passing the row index and column index, both start at 0. Hence a row index of 2 and a column index of 3 places widget in 3rd column of 2nd row. Two geometry windows are being used, with each frame being attached to the window
Here, even though .grid() is called on each object, geometry manager applies only to window objects. Frames in the previous example are placed next to each other. To add space around frame, the padding of each cell can be fixed.
Two types of padding are ‘External Padding’ and ‘Internal Padding’, External padding adds space outside of grid. padx() and pady() adds padding horizontally and vertically. Both are measured in pixels, so setting these values will make padding in both directions.
Example #5: Tkinter Table with List of Employees
Code:
from tkinter import *
class Tkinter_Table:
def __init__(table_text,root):
for x in range(rows):
for y in range(columns):
table_text.e = Entry(root, width=15, fg='blue', font=('Timew New Roman',13,'italic'))
table_text.e.grid(row=x, column=y)
table_text.e.insert(END, employee_array[x][y])
employee_array = [(101,'Karthick','Delhi',20), (102,'Saideep','Mumbai',21), (103,'Anusha','Ahmedabad',23), (104,'Riya','Surat',25), (105,'Siva','Nagpur',29), (106,'Bhargav','Pune',19)]
rows = len(employee_array)
columns = len(employee_array[0])
root = Tk()
t = Tkinter_Table(root)
root.mainloop()
Output:
Most important of grid() manager in Tkinter are the below 3 keywords,
- Place: Used to position widgets at their absolute location.
- Grid: Used to arrange widgets in grid.
- Pack: Used to pack the mentioned widgets into a frame or a cavity.
With this, we conclude our topic ‘Python Tkinter’. We have seen what Python Tkinter is and how it is used in GUI applications. Syntax of Tkinter is also explained here, you can start by Installing Python onto your systems and just go with the coding. We have also seen some of the examples for your understanding. You can still explore some of the other widgets of Tkinter used in GUI applications using Python. Thanks! Happy Learning!!
Recommended Articles
We hope that this EDUCBA information on “Tkinter Table” was beneficial to you. You can view EDUCBA’s recommended articles for more information.