Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 467
IordanouGiannis Avatar asked Aug 26 '11 18:08

IordanouGiannis


People also ask

Does Tkinter have table widget?

Unfortunately, Tkinter does not provide a Table widget to create a table. But we can create a table using alternate methods. For example, we can make a table by repeatedly displaying entry widgets in the form of rows and columns.

Is there a widget for Excel?

Widgets are inserted in the spreadsheet from the Widgets tab of the SpreadsheetConverter task pane in Excel. Select the cell where you want to insert the widget; then select the type of widget from the task pane. An options dialog appears, in which you can specify the detailed settings for the widget.

What is the best way to show data in a table in Tkinter?

Luckily, there are alternate methods for creating a table to display data in Tkinter. For example, the Entry widget can be coded to display data in a table, and there are also table packages that can be downloaded from the Python Package Index (PyPI) and installed.

What widgets are available in Tkinter?

Using Ttk. That code causes several tkinter. ttk widgets ( Button , Checkbutton , Entry , Frame , Label , LabelFrame , Menubutton , PanedWindow , Radiobutton , Scale and Scrollbar ) to automatically replace the Tk widgets.


2 Answers

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

like image 78
Bryan Oakley Avatar answered Oct 21 '22 22:10

Bryan Oakley


You can use Tkinter like so 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()

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()
like image 22
Steven Avatar answered Oct 21 '22 21:10

Steven