66

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?

Ethan Field
  • 4,646
  • 3
  • 22
  • 43
Freewind
  • 193,756
  • 157
  • 432
  • 708
  • https://github.com/clarenceangel/tkinterstuff easy stuff perfect for beginners does exactly what you want. – Mixstah Jun 13 '17 at 22:51
  • https://www.youtube.com/watch?v=i4qLI9lmkqw This is imo the best table you can make with tkinter. –  Oct 01 '20 at 08:07

10 Answers10

49

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.

Yury Wallet
  • 1,474
  • 1
  • 13
  • 24
Steven
  • 790
  • 7
  • 16
27

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".

nbro
  • 15,395
  • 32
  • 113
  • 196
Sticky
  • 1,022
  • 1
  • 11
  • 19
  • 11
    Can't believe such a basic GUI display element such as a table is not included. C'mon tkinter... – NoName Oct 12 '19 at 20:41
20

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

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
14

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

ragardner
  • 1,836
  • 5
  • 22
  • 45
  • 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 '20 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 '20 at 10:58
  • Do you think you could add a texteditor example to your docs? – dataman Sep 28 '20 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 '22 at 00:10
14

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

Wolf
  • 9,679
  • 7
  • 62
  • 108
chido
  • 141
  • 2
  • 6
    This now redirects to https://github.com/dmnfarrell/tkintertable where it states it's **for Python 2 only** – handle Jun 24 '16 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 '17 at 22:24
  • 1
    if your using pandas, this one might be a good option: https://github.com/dmnfarrell/pandastable – stevenferrer Feb 09 '18 at 10:29
  • I observe no issues with python 3 so the comment by @handle seems outdated now. – Wolf May 13 '21 at 22:32
  • This library is now Python 3 compatible! – Thierryonre Jan 04 '23 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()
Jojojustin
  • 51
  • 1
  • 5
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.

Mixstah
  • 411
  • 1
  • 7
  • 22
3

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

Sam Varghese
  • 428
  • 1
  • 5
  • 14
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
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
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()
SAR
  • 180
  • 1
  • 9