13

I want a Excel like table widget in tkinter for a gui I am writing. Do you have any suggestions?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
IordanouGiannis
  • 4,149
  • 16
  • 65
  • 99

3 Answers3

15

You can use Tkinter to make a simple spreadsheet-like GUI:

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

If you want to get the values from the grid, you can use the grid's children.

def find_in_grid(frame, row, column):
    for child in frame.children.values():
        info = child.grid_info()
        if info['row'] == row and info['column'] == column:
            return child
    return None

The function will return the child. To get the value of the entry, you can use:

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

Note: In old versions of Tkinter, row and column are stored as strings, so there you'd need to cast the integers:

if info['row'] == str(row) and info['column'] == str(column):
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Steven
  • 790
  • 7
  • 16
  • I'm glad for your starting point, but it doesn't work unless you also use `StringVar` for each `Entry` – nmz787 Mar 08 '18 at 02:12
9

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 tk
import tktable

root = 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)?

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

I had a similar problem trying to show a complete panda's DataFrame using tkinter. My first option was to create a grid like suggested someone in the first answer, but it bacame too heavy for so many info.

I guess is beter to create a finite numbers of cells and show only a part of the DF, changing that part with the movement of the user