Does tkinter have a table widget?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

Method 3

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

Method 4

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

Method 5

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

Does tkinter have a table widget?

Method 6

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()

Method 7

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.

Method 8

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:

Does tkinter have a table widget?

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

Method 9

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

Method 10

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()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x