Which widget do you use for a Excel like table in tkinter? Which widget do you use for a Excel like table in tkinter? tkinter tkinter

Which widget do you use for a Excel like table in tkinter?


You can use Tkinter like so to make a simple spreadsheet like gui:

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

Edit:If you wanted to get the values from the grid, you have to use the grid's children.

def find_in_grid(frame, row, column):    for children in frame.children.values():        info = children.grid_info()        #note that rows and column numbers are stored as string                                                                                 if info['row'] == str(row) and info['column'] == str(column):            return children    return None

Where you can call the function and it will return the child. To get the value of the entry, you can use:

find_in_grid(root, i+1, j).get()


Tktable is at least arguably the best option, if you need full table support. Briefly, the following example shows how to use it assuming you have it installed. The example is for python3, but for python2 you only need to change the import statement.

import tkinter as tkimport tktableroot = tk.Tk()table = tktable.Table(root, rows=10, cols=4)table.pack(side="top", fill="both", expand=True)root.mainloop()

Tktable can be difficult to install since there is no pip-installable package.

If all you really need is a grid of widgets for displaying and editing data, you can easily build a grid of entry or label widgets. For an example, see this answer to the question Python. GUI(input and output matrices)?