Tableview of ttkbootstrap


Ttkbootstrap Table

bootstyleStyle keyword, options are
primary, secondary, success,info,warning,danger, light, dark
coldata Names or Dictionary of column names with settings.
rowdataIterable rows with data. Must match with number of columns
paginatedAdding Pagination of records - required or not. ( True / False )
pagesizeNumber of rows to show per page, works with paginated
check the difference between pagesize and height below.
searchable Show search entry or not
autofit If True, columns will be automatically sized.
autoalign If True, Numbers are right aligned and others are left aligned.
stripecolorTuple to provide alternate row colours. ('lightblue',None)
heightNumber of rows to display in Viewport. Check below for details.
delimiter Char to use as delimiter ( default is comma ) while esporting data.


Ttkbootstrap tableview to display tabular data with parameters to manage properties

Example with List of Columns and data Rows 🔝

Here we are creating two different Lists. The list l1 is the column header text with dictionay of style properties.
One more list r_set contains the rows of data.
We can change the parameters of the table and observe the effects.
import ttkbootstrap as ttk
from ttkbootstrap.tableview import Tableview
from ttkbootstrap.constants import *

my_w = ttk.Window()
my_w.geometry("400x300")  # width and height
colors = my_w.style.colors
l1 = [
    {"text": "id", "stretch": False},
    {"text":"Name","stretch":True},
    "Class",
    {"text":"Mark"},
    {"text":"Gender"}
]  # Columns with Names and style 
# Data rows as list 
r_set = [(1, "Alex", 'Four',90,'Female'), (2, "Ron", "Five",80,'Male'), 
            (3, "Geek", 'Four',70,'Male'),(4,'King','Five',78,'Female'),
            (5,'Queen','Four',60,'Female'),(6,'Jack','Five',70,'Female')]

dv = ttk.tableview.Tableview(
    master=my_w,
    paginated=True,
    coldata=l1,
    rowdata=r_set,
    searchable=True,
    bootstyle=SUCCESS,
    pagesize=10,
    height=10,
    stripecolor=(colors.light, None),
)
dv.grid(row=0, column=0, padx=10, pady=5)
dv.autofit_columns() # Fit in current view 
my_w.mainloop()

Summary row: Adding extra record by insert_row() 🔝

Summary row showing sum of marks at end of Tableview of Tkinter ttkbootstrap


After the table is displayed we can add one new row at the end of the table showing the summary of data. Here we are showing the total of the mark column values. To get the total value we have used this code.
marks=[r[3] for r in r_set] # List of all marks column
print(sum(marks)) # sum of the marks column
After the table is added we have used insert_row() to add the row at the end and used load_table_data() to load all records to table view.
Ttkboostrap Table with summary row at end

Full code is here
import ttkbootstrap as ttk
from ttkbootstrap.tableview import Tableview
from ttkbootstrap.constants import *

my_w = ttk.Window()
my_w.geometry("400x300")  # width and height
colors = my_w.style.colors
l1 = [
    {"text": "id", "stretch": False},
    {"text":"Name","stretch":True},
    "Class",
    {"text":"Mark"},
    {"text":"Gender"}
]  # Columns with Names and style 
# Data rows as list 
r_set = [(1, "Alex", 'Four',90,'Female'), (2, "Ron", "Five",80,'Male'), 
            (3, "Geek", 'Four',70,'Male'),(4,'King','Five',78,'Female'),
            (5,'Queen','Four',60,'Female'),(6,'Jack','Five',70,'Female')]
marks=[r[3] for r in r_set] # List of all marks column
print(sum(marks)) # sum of the marks column 
dv = ttk.tableview.Tableview(
    master=my_w,
    paginated=True,
    coldata=l1,
    rowdata=r_set,
    searchable=True,
    bootstyle=SUCCESS,
    pagesize=10,
    height=10,
    stripecolor=(colors.light, None),
)
dv.grid(row=0, column=0, padx=10, pady=5)
dv.autofit_columns() # Fit in current view 
dv.insert_row("end", values=['-', "---", "All", sum(marks), "All"])
dv.load_table_data() # Load all data rows 
my_w.mainloop()
We can add column and rows after initialization of the table
dv.build_table_data(l1,r_set)
dv.autofit_columns() # Fit in current view 
dv.load_table_data() # Load all data rows 

Paging of records 🔝

ttkboostrap tableview with paging
If we have large number of rows and the view port ( height parameter ) is less, then we can display some records within the view port and provide user with navigation to move to different part of the available records.
Read more about PHP Paging here .
By making the parameter paginated=True , we can show navigation buttons to move to different pages of the records. Here the parameter pagesize=10 will accommodate 10 records per page.

pagesize & height 🔝

Pagesize: number of records visible per page.
height : number of rows within Table's viewport.

If Pagesize is more than height then user has to scroll using mousewheel to naviage.

Updating default font 🔝

Use these lines after declaring the root window to change the font details.
from tkinter.font import nametofont
default_font = nametofont("TkDefaultFont")
default_font.configure(family="Times",size=14,weight='bold')

Summary row and paging of records 🔝

If we are adding any row by using insert_row() as explained above and using paginated=True then the row will be added at the last page only.

Export Rows 🔝

Right click on the table rows and we can export data using the set delimiter.
By default we can download data as CSV file.
Exporting records in Tableview

Export selectd Rows 🔝

We can use export_current_selection() to export the selected rows to CSV file. Hold the Ctrl key to select multiple rows.
b1=ttk.Button(my_w,text='Export',bootstyle=SUCCESS,
    command=lambda:dv.export_current_selection())
b1.grid(row=1,column=0)

Getting row value 🔝

row = dv.tablerows
print(row[1].values) # [2, 'Max Ruin', 'Three', 85, 'male']

Delete row 🔝

b1=ttk.Button(my_w,text='Export',bootstyle=SUCCESS,
    command=lambda:dv.delete_row(5))

Hide and show a column of table view by using checkbutton 🔝

Show hide column using checkbutton

Show or hide column by using checkbutton in a Tkinter ttkbootstrap tableview
import ttkbootstrap as ttk
from ttkbootstrap.tableview import Tableview
from ttkbootstrap.constants import *

my_w = ttk.Window()
my_w.geometry("400x350")  # width and height
colors = my_w.style.colors
l1 = [
    {"text": "id", "stretch": False},
    {"text":"Name","stretch":True},
    "Class",
    {"text":"Mark"},
    {"text":"Gender"}
]  # Columns with Names and style 
# Data rows as list 
r_set1 = [(1, "Alex", 'Four',90,'Female'), (2, "Ron", "Five",80,'Male'), 
            (3, "Geek", 'Four',70,'Male'),(4,'King','Five',78,'Female'),
            (5,'Queen','Four',60,'Female'),(6,'Jack','Five',70,'Female')]
l2 = [
    {"text": "id", "stretch": False},
    {"text":"Name","stretch":True},
    "Class",
    {"text":"Mark"}
    
]  # Columns with Names and style 
# Data rows as list 
r_set2 = [(1, "Alex", 'Four',90), (2, "Ron", "Five",80), 
            (3, "Geek", 'Four',70),(4,'King','Five',78),
            (5,'Queen','Four',60),(6,'Jack','Five',70)]
dv = ttk.tableview.Tableview(
    master=my_w,
    paginated=True,
    coldata=l1,
    rowdata=r_set1,
    searchable=True,
    bootstyle=SUCCESS,
    pagesize=10,
    height=10,
    stripecolor=(colors.light, None),
)
dv.grid(row=0, column=0, padx=10, pady=5)
#dv.build_table_data(l2,r_set2)
dv.autofit_columns() # Fit in current view 
def my_upd():

    dv.grid_forget()
    if cb1_v.get():
        dv.build_table_data(l1,r_set1)
    else:
        dv.build_table_data(l2,r_set2)
    dv.autofit_columns() # Fit in current view 
    dv.load_table_data() # Load all data rows 
    dv.grid(row=0, column=0, padx=10, pady=5)
cb1_v = ttk.BooleanVar(value=True)
cb1 = ttk.Checkbutton(variable=cb1_v,text="Gender", 
	onvalue=True, offvalue=False, bootstyle=SUCCESS ,command=my_upd)
cb1.grid(row=1,column=0)

my_w.mainloop()
Different data sources for Tableview
ttkbootstrap
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer