67

I'm learning Python, and I would like to use it to create a simple GUI application, and since Tkinter is already built-in (and very simple to use) I would like to use it to build my application.

I would like to make an app that will display a table that contains some data that I've loaded from my database.

I've searched for table but have not been able to find any examples and / or documentation regarding a Tkinter table component.

Does Tkinter have a built in table component? If not, what could I / should I use instead?

2

10 Answers 10

50

You can use Tkinter's grid.

To create a simple excel-like table:

try:
    from tkinter import * 
except ImportError:
    from Tkinter import *

root = Tk()

height = 5
width = 5
for i in range(height): #Rows
    for j in range(width): #Columns
        b = Entry(root, text="")
        b.grid(row=i, column=j)

mainloop()

You can grab the data by accessing the children of the grid and getting the values from there.

4
  • I think you forgot to add a b.pack().
    – Sam Chats
    Nov 7, 2017 at 18:08
  • 30
    No, you should never mix grid and pack. Mar 26, 2018 at 23:14
  • 2
    if I create a table like this how will I reference the fields after creation? If I got it right you are saying I can reference stuff with the grid coordinates? Jul 17, 2018 at 16:44
  • 2
    @Steven: What does the "b" stand for? Is this a conventional name for Entry? Sep 25, 2018 at 15:31
29

Tkinter doesn't have a built-in table widget. The closest you can use is a Listbox or a Treeview of the tkinter's sub package ttk.

However, you can use tktable, which is a wrapper around the Tcl/Tk TkTable widget, written by Guilherme Polo. Note: to use this wrapper library you need first to have installed the original Tk's TkTable library, otherwise you will get an "import error".

1
  • 12
    Can't believe such a basic GUI display element such as a table is not included. C'mon tkinter...
    – NoName
    Oct 12, 2019 at 20:41
21

If the table is read-only and you're using a sufficiently modern version of Tkinter you can use the ttk.Treeview widget.

You can also pretty easily create a grid of Entry or Label widgets. See this answer for an example: https://stackoverflow.com/a/11049650/7432

1
16

I am the author of a tkinter table widget written in pure python named tksheet it only works for Python 3.63+

It works using tkinter canvases and only redraws the visible portion of the table so it runs pretty smoothly even with hundreds of millions of cells

Among many other features you can also change any colors easily and highlight cells background and foreground

You can find the repository here: https://github.com/ragardner/tksheet

tksheet basic example

4
  • 1
    (I know this is irrelevant to this particular post but oh well) I've just started using your widget and it's amazing, however one thing I can't get to work is centering the actual sheet. I have the align = 'center', but having trouble centering the table within a frame using pack
    – Sean2148
    Jun 11, 2020 at 16:48
  • @Sean2148 Actually, a correction to my first answer - You can center a widget in the y and x axis but not with pack only with .grid()
    – ragardner
    Jun 13, 2020 at 10:58
  • Do you think you could add a texteditor example to your docs?
    – dataman
    Sep 28, 2020 at 13:27
  • Are there videos demonstrating interactions with tksheet, for example selecting several non-consecutive rows with Ctrl+click, then dragging&dropping them so that the order of rows changes, etc.?
    – root
    Feb 14, 2022 at 0:10
15

You could use tkintertable. See the wiki how to start using it.

5
  • 6
    This now redirects to github.com/dmnfarrell/tkintertable where it states it's for Python 2 only
    – handle
    Jun 24, 2016 at 13:44
  • 1
    Freewind: Even though tkintertable is written for Python 2, it appears to be pure Python—so, since it's open source, updating it to work in Python 3 might not be too difficult.
    – martineau
    Jan 21, 2017 at 22:24
  • 1
    if your using pandas, this one might be a good option: github.com/dmnfarrell/pandastable Feb 9, 2018 at 10:29
  • I observe no issues with python 3 so the comment by @handle seems outdated now.
    – Wolf
    May 13, 2021 at 22:32
  • This library is now Python 3 compatible! Jan 4 at 18:01
5

In addition to @steven response, you can do this to reference any table cell

from Tkinter import *

root = Tk()

height = 5
width = 5
cells = {}
for i in range(height): #Rows
    for j in range(width): #Columns
        b = Entry(root, text="")
        b.grid(row=i, column=j)
        cells[(i,j)] = b

mainloop()
4

Using prettytable Library

Hi everyone , we can use prettytable library in order to make great tables like that of sql in tkinter.

Firstly execute the following code in cmd to install prettytable library

pip install prettytable

now here is a self explanatory code for making table:

from prettytable import PrettyTable
from tkinter import *

win=Tk()

t=Text(win)#Inside text widget we would put our table

x=PrettyTable()

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]

x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])

t.insert(INSERT,x)#Inserting table in text widget
t.pack()

win.mainloop()

Output of this code:

enter image description here

Put the following line of code after t.insert(INSERT,x) in order to make this table read-only

t.config(state=DISABLED)

This method would make tables very easily. If you are curious to know more about prettytable , click here

3

https://github.com/clarenceangel/tkinterstuff i made this but I am no pro. It does create a table though and return it as a frame that you can add to a frame or root.You feed it a csv with any number of rows and columns so long as the columns are even on each row of course.

1

you can try the tksheet widget, it is just like excel files in tkinter. in which you can make tables also. if you use windows you can install it using,

pip install tksheet

and when importing in tkinter you can use,

from tksheet import Sheet

import tkinter as tk
0

Adding to @Jojojustin and @Steven - making the "table" responsive

from tkinter import * 
root = Tk()

row = 5
col = 5
cells = {}
for i in range(row):
    for j in range(col):
        root.columnconfigure(j,weight=20) # making the columns responsive 
        root.rowconfigure(i,weight=20) # making the rows responsive 
        b = Entry(root,text="")
        b.grid(row=i,column=j,sticky=NSEW)
        cells[(i,j)] = b
root.mainloop()

Not the answer you're looking for? Browse other questions tagged or ask your own question.