Reputation: 198238
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?
Upvotes: 72
Views: 229831
Reputation: 1975
I am the author of a tkinter table widget written in pure python named tksheet
. It only works for Python 3.8+.
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
Upvotes: 24
Reputation: 310
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()
Upvotes: 0
Reputation: 468
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()
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
Upvotes: 6
Reputation: 11
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
Upvotes: 1
Reputation: 820
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.
Upvotes: 52
Reputation: 51
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()
Upvotes: 5
Reputation: 411
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.
Upvotes: 3
Reputation: 1082
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".
Upvotes: 30
Reputation: 386010
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
Upvotes: 24