Which widget do you use for an Excel-like table in Tkinter?

TkinterPythonGUI-Programming

Tkinter is a standard Python library that is used to build featured GUI-based desktop applications. Tkinter itself offers a variety of functions and widgets that can be used to design and serve the application's needs.

Tkinter Treeview widget is one of the consistent widgets which is used for driving the data and information in the form of a table. It works similar to MS Excel where we can add or define columns, insert some values to it, and perform queries in the backend using other Python libraries (such as Numpy or Pandas).

Tkinter Treeview widget is created by defining the Treeview(parent, column=(**col), **options) constructor. Now we can add columns and insert values to it.

Example

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win= Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Add a Treeview widget
tree= ttk.Treeview(win, column=("c1", "c2","c3"), show= 'headings')
tree.column("# 1",anchor=CENTER)
tree.heading("# 1", text= "ID")
tree.column("# 2", anchor= CENTER)
tree.heading("# 2", text= "FName")
tree.column("# 3", anchor= CENTER)
tree.heading("# 3", text="LName")

# Insert the data in Treeview widget
tree.insert('', 'end',text= "1",values=('XYZ', 'ABC','123'))
tree.pack()

win.mainloop()

Output

Running the above code will display a table-like window with columns and entries for the row.

raja
Published on 08-Jun-2021 07:19:00
Advertisements