bootstyle | Style keyword, options are primary, secondary, success,info,warning,danger, light, dark |
coldata | Names or Dictionary of column names with settings. |
rowdata | Iterable rows with data. Must match with number of columns |
paginated | Adding Pagination of records - required or not. ( True / False ) |
pagesize | Number 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. |
stripecolor | Tuple to provide alternate row colours. ('lightblue',None) |
height | Number of rows to display in Viewport. Check below for details. |
delimiter | Char to use as delimiter ( default is comma ) while exporting data. |
import ttkbootstrap as ttk from ttkbootstrap.tableview import Tableview from ttkbootstrap.constants import *
my_w = ttk.Window() my_w.geometry("400x300") # Set width and height colors = my_w.style.colors
l1 = [ {"text": "id", "stretch": False}, {"text": "Name", "stretch": True}, "Class", {"text": "Mark"}, {"text": "Gender"} ]
r_set = [ (1, "Alex", "Four", 90, "Female"), (2, "Ron", "Five", 80, "Male"), (3, "Geek", "Four", 70, "Male"), (4, "King", "Five", 78, "Female") ]
dv = 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()
my_w.mainloop()
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')]
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()
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. 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
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.
from tkinter.font import nametofont
default_font = nametofont("TkDefaultFont")
default_font.configure(family="Times",size=14,weight='bold')
paginated=True
then the row will be added at the last page only.
b1=ttk.Button(my_w,text='Export',bootstyle=SUCCESS,
command=lambda:dv.export_current_selection())
b1.grid(row=1,column=0)
row = dv.tablerows
print(row[1].values) # [2, 'Max Ruin', 'Three', 85, 'male']
b1=ttk.Button(my_w,text='Export',bootstyle=SUCCESS,
command=lambda:dv.delete_row(5))
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()
import ttkbootstrap as ttk
from ttkbootstrap.tableview import Tableview
from ttkbootstrap.constants import *
my_w = ttk.Window()
my_w.geometry("400x320")
colors = my_w.style.colors
l1 = [
{"text": "id", "stretch": False},
{"text": "Name", "stretch": True},
"Class",
{"text": "Mark"},
{"text": "Gender"}
]
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 = 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()
label = ttk.Label(my_w, text="Selected Row ID: None", bootstyle="info")
label.grid(row=1, column=0, padx=10, pady=5)
def on_row_select(event):
selected_item = dv.view.selection()
if selected_item:
row_id = dv.view.item(selected_item[0])["values"][0]
label.configure(text=f"Selected Row ID: {row_id}")
dv.view.bind("<<TreeviewSelect>>", on_row_select)
my_w.mainloop()
To select multiple rows by holding the Ctrl, here is the modified code.
def on_row_select(event):
selected_item = dv.view.selection()
if selected_item:
# row_id = dv.view.item(selected_item[0])["values"][0]
# label.configure(text=f"Selected Row ID: {row_id}")
row_ids = [dv.view.item(item)["values"][0] for item in selected_item]
label.configure(text=f"Selected Row IDs: {', '.join(map(str, row_ids))}")