This recipe makes easy to work a treeview widget like a table.
It has several options for styling:
- heading_anchor
- heading_font
- heading_background
- heading_foreground
- cell_anchor
- cell_background
- cell_foreground
- cell_font
- cell_pady
- height
- padding
- adjust_heading_to_content
- stripped_rows
- headers
- selection_background
- selection_foreground
- field_background
The "command" parameter is a callback and its called each time a row is selected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 | # Version: 2.3
# Author: Miguel Martinez Lopez
# Uncomment the next line to see my email
# print("Author's email: %s"%"61706c69636163696f6e616d656469646140676d61696c2e636f6d".decode("hex"))
try:
from Tkinter import Frame, Label
from Tkconstants import *
from tkFont import Font, nametofont
from ttk import Treeview, Style
except ImportError:
from tkinter import Frame, Label
from tkinter.constants import *
from tkinter.font import Font, nametofont
from tkinter.ttk import Treeview, Style
# Python 3 compatibility
try:
basestring
except NameError:
basestring = str
class Row(object):
def __init__(self, table, index):
self._multicolumn_listbox = table
self._index = index
def data(self):
return self._multicolumn_listbox.row_data(self._index)
def delete(self):
self._multicolumn_listbox.delete_row(self._index)
def update(self, data):
self._multicolumn_listbox.update_row(self._index, data)
def select(self):
self._multicolumn_listbox.select_row(self._index)
def deselect(self):
self._multicolumn_listbox.deselect_row(self._index)
def __str__(self):
return str(self.data())
def __len__(self):
return self._multicolumn_listbox.number_of_columns
class Column(object):
def __init__(self, table, index):
self._multicolumn_listbox = table
self._index = index
def data(self):
return self._multicolumn_listbox.column_data(self._index)
def delete(self):
self._multicolumn_listbox.delete_column(self._index)
def update(self, data):
self._multicolumn_listbox.update_column(self._index, data)
def __str__(self):
return str(self.data())
def __len__(self):
return self._multicolumn_listbox.number_of_rows
class Multicolumn_Listbox(object):
_style_index = 0
class List_Of_Rows(object):
def __init__(self, multicolumn_listbox):
self._multicolumn_listbox = multicolumn_listbox
def data(self, index):
return self._multicolumn_listbox.row_data(index)
def get(self, index):
return Row(self._multicolumn_listbox, index)
def insert(self, data, index=None):
self._multicolumn_listbox.insert_row(data, index)
def delete(self, index):
self._multicolumn_listbox.delete_row(index)
def update(self, index, data):
self._multicolumn_listbox.update_row(index, data)
def select(self, index):
self._multicolumn_listbox.select_row(index)
def deselect(self, index):
self._multicolumn_listbox.deselect_row(index)
def set_selection(self, indices):
self._multicolumn_listbox.set_selection(indices)
def __getitem__(self, index):
return self.get(index)
def __setitem__(self, index, value):
return self._multicolumn_listbox.update_row(index, value)
def __delitem__(self, index):
self._multicolumn_listbox.delete_row(index)
def __len__(self):
return self._multicolumn_listbox.number_of_rows
class List_Of_Columns(object):
def __init__(self, multicolumn_listbox):
self._multicolumn_listbox = multicolumn_listbox
def data(self, index):
return self._multicolumn_listbox.get_column(index)
def get(self, index):
return Column(self._multicolumn_listbox, index)
def delete(self, index):
self._multicolumn_listbox.delete_column(index)
def update(self, index, data):
self._multicolumn_listbox.update_column(index, data)
def __getitem__(self, index):
return self.get(index)
def __setitem__(self, index, value):
return self._multicolumn_listbox.update_column(index, value)
def __delitem__(self, index):
self._multicolumn_listbox.delete_column(index)
def __len__(self):
return self._multicolumn_listbox.number_of_columns
def __init__(self, master, columns, data=None, command=None, sort=True, select_mode=None, heading_anchor = CENTER, cell_anchor=W, style=None, height=None, padding=None, adjust_heading_to_content=False, stripped_rows=None, selection_background=None, selection_foreground=None, field_background=None, heading_font= None, heading_background=None, heading_foreground=None, cell_pady=2, cell_background=None, cell_foreground=None, cell_font=None, headers=True):
self._stripped_rows = stripped_rows
self._columns = columns
self._number_of_rows = 0
self._number_of_columns = len(columns)
self.row = self.List_Of_Rows(self)
self.column = self.List_Of_Columns(self)
s = Style()
if style is None:
style_name = "Multicolumn_Listbox%s.Treeview"%self._style_index
self._style_index += 1
else:
style_name = style
style_map = {}
if selection_background is not None:
style_map["background"] = [('selected', selection_background)]
if selection_foreground is not None:
style_map["foeground"] = [('selected', selection_foreground)]
if style_map:
s.map(style_name, **style_map)
style_config = {}
if cell_background is not None:
style_config["background"] = cell_background
if cell_foreground is not None:
style_config["foreground"] = cell_foreground
if cell_font is None:
font_name = s.lookup(style_name, "font")
cell_font = nametofont(font_name)
else:
if not isinstance(cell_font, Font):
if isinstance(cell_font, basestring):
cell_font = nametofont(cell_font)
else:
if len(font) == 1:
cell_font = Font(family=cell_font[0])
elif len(font) == 2:
cell_font = Font(family=cell_font[0], size=cell_font[1])
elif len(font) == 3:
cell_font = Font(family=cell_font[0], size=cell_font[1], weight=cell_font[2])
else:
raise ValueError("Not possible more than 3 values for font")
style_config["font"] = cell_font
self._cell_font = cell_font
self._rowheight = cell_font.metrics("linespace")+cell_pady
style_config["rowheight"]=self._rowheight
if field_background is not None:
style_config["fieldbackground"] = field_background
s.configure(style_name, **style_config)
heading_style_config = {}
if heading_font is not None:
heading_style_config["font"] = heading_font
if heading_background is not None:
heading_style_config["background"] = heading_background
if heading_foreground is not None:
heading_style_config["foreground"] = heading_foreground
heading_style_name = style_name + ".Heading"
s.configure(heading_style_name, **heading_style_config)
treeview_kwargs = {"style": style_name}
if height is not None:
treeview_kwargs["height"] = height
if padding is not None:
treeview_kwargs["padding"] = padding
if headers:
treeview_kwargs["show"] = "headings"
else:
treeview_kwargs["show"] = ""
if select_mode is not None:
treeview_kwargs["selectmode"] = select_mode
self.interior = Treeview(master, columns=columns, **treeview_kwargs)
if command is not None:
self._command = command
self.interior.bind("<<TreeviewSelect>>", self._on_select)
for i in range(0, self._number_of_columns):
if sort:
self.interior.heading(i, text=columns[i], anchor=heading_anchor, command=lambda col=i: self.sort_by(col, descending=False))
else:
self.interior.heading(i, text=columns[i], anchor=heading_anchor)
if adjust_heading_to_content:
self.interior.column(i, width=Font().measure(columns[i]))
self.interior.column(i, anchor=cell_anchor)
if data is not None:
for row in data:
self.insert_row(row)
@property
def row_height(self):
return self._rowheight
@property
def font(self):
return self._cell_font
def configure_column(self, index, width=None, minwidth=None, anchor=None, stretch=None):
kwargs = {}
for config_name in ("width", "anchor", "stretch", "minwidth"):
config_value = locals()[config_name]
if config_value is not None:
kwargs[config_name] = config_value
self.interior.column('#%s'%(index+1), **kwargs)
def row_data(self, index):
try:
item_ID = self.interior.get_children()[index]
except IndexError:
raise ValueError("Row index out of range: %d"%index)
return self.item_ID_to_row_data(item_ID)
def update_row(self, index, data):
try:
item_ID = self.interior.get_children()[index]
except IndexError:
raise ValueError("Row index out of range: %d"%index)
if len(data) == len(self._columns):
self.interior.item(item_ID, values=data)
else:
raise ValueError("The multicolumn listbox has only %d columns"%self._number_of_columns)
def delete_row(self, index):
list_of_items = self.interior.get_children()
try:
item_ID = list_of_items[index]
except IndexError:
raise ValueError("Row index out of range: %d"%index)
self.interior.delete(item_ID)
self._number_of_rows -= 1
if self._stripped_rows:
for i in range(index, self._number_of_rows):
self.interior.tag_configure(list_of_items[i+1], background=self._stripped_rows[i%2])
def insert_row(self, data, index=None):
if len(data) != self._number_of_columns:
raise ValueError("The multicolumn listbox has only %d columns"%self._number_of_columns)
if index is None:
index = self._number_of_rows-1
item_ID = self.interior.insert('', index, values=data)
self.interior.item(item_ID, tags=item_ID)
self._number_of_rows += 1
if self._stripped_rows:
list_of_items = self.interior.get_children()
self.interior.tag_configure(item_ID, background=self._stripped_rows[index%2])
for i in range(index+1, self._number_of_rows):
self.interior.tag_configure(list_of_items[i], background=self._stripped_rows[i%2])
def column_data(self, index):
return [self.interior.set(child_ID, index) for child_ID in self.interior.get_children('')]
def update_column(self, index, data):
for i, item_ID in enumerate(self.interior.get_children()):
data_row = self.item_ID_to_row_data(item_ID)
data_row[index] = data[i]
self.interior.item(item_ID, values=data_row)
return data
def clear(self):
# Another possibility:
# self.interior.delete(*self.interior.get_children())
for row in self.interior.get_children():
self.interior.delete(row)
self._number_of_rows = 0
def update(self, data):
self.clear()
for row in data:
self.insert_row(row)
def focus(self, index=None):
if index is None:
return self.interior.item(self.interior.focus())
else:
item = self.interior.get_children()[index]
self.interior.focus(item)
def state(self, state=None):
if stateSpec is None:
return self.interior.state()
else:
self.interior.state(state)
@property
def number_of_rows(self):
return self._number_of_rows
@property
def number_of_columns(self):
return self._number_of_columns
def toogle_selection(self, index):
list_of_items = self.interior.get_children()
try:
item_ID = list_of_items[index]
except IndexError:
raise ValueError("Row index out of range: %d"%index)
self.interior.selection_toggle(item_ID)
def select_row(self, index):
list_of_items = self.interior.get_children()
try:
item_ID = list_of_items[index]
except IndexError:
raise ValueError("Row index out of range: %d"%index)
self.interior.selection_add(item_ID)
def deselect_row(self, index):
list_of_items = self.interior.get_children()
try:
item_ID = list_of_items[index]
except IndexError:
raise ValueError("Row index out of range: %d"%index)
self.interior.selection_remove(item_ID)
def deselect_all(self):
self.interior.selection_remove(self.interior.selection())
def set_selection(self, indices):
list_of_items = self.interior.get_children()
self.interior.selection_set(" ".join(list_of_items[row_index] for row_index in indices))
@property
def selected_rows(self):
data = []
for item_ID in self.interior.selection():
data_row = self.item_ID_to_row_data(item_ID)
data.append(data_row)
return data
@property
def indices_of_selected_rows(self):
list_of_indices = []
for index, item_ID in enumerate(self.interior.get_children()):
if item_ID in self.interior.selection():
list_of_indices.append(index)
return list_of_indices
def delete_all_selected_rows(self):
selected_items = self.interior.selection()
for item_ID in selected_items:
self.interior.delete(item_ID)
number_of_deleted_rows = len(selected_items)
self._number_of_rows -= number_of_deleted_rows
return number_of_deleted_rows
def _on_select(self, event):
for item_ID in event.widget.selection():
data_row = self.item_ID_to_row_data(item_ID)
self._command(data_row)
def item_ID_to_row_data(self, item_ID):
item = self.interior.item(item_ID)
return item["values"]
@property
def table_data(self):
data = []
for item_ID in self.interior.get_children():
data_row = self.item_ID_to_row_data(item_ID)
data.append(data_row)
return data
@table_data.setter
def table_data(self, data):
self.update(data)
def cell_data(self, row, column):
"""Get the value of a table cell"""
try:
item = self.interior.get_children()[row]
except IndexError:
raise ValueError("Row index out of range: %d"%row)
return self.interior.set(item, column)
def update_cell(self, row, column, value):
"""Set the value of a table cell"""
item_ID = self.interior.get_children()[row]
data = self.item_ID_to_row_data(item_ID)
data[column] = value
self.interior.item(item_ID, values=data)
def __getitem__(self, index):
if isinstance(index, tuple):
row, column = index
return self.cell_data(row, column)
else:
raise Exception("Row and column indices are required")
def __setitem__(self, index, value):
if isinstance(index, tuple):
row, column = index
self.update_cell(row, column, value)
else:
raise Exception("Row and column indices are required")
def bind(self, event, handler):
self.interior.bind(event, handler)
def sort_by(self, col, descending):
"""
sort tree contents when a column header is clicked
"""
# grab values to sort
data = [(self.interior.set(child_ID, col), child_ID) for child_ID in self.interior.get_children('')]
# if the data to be sorted is numeric change to float
try:
data = [(float(number), child_ID) for number, child_ID in data]
except ValueError:
pass
# now sort the data in place
data.sort(reverse=descending)
for idx, item in enumerate(data):
self.interior.move(item[1], '', idx)
# switch the heading so that it will sort in the opposite direction
self.interior.heading(col, command=lambda col=col: self.sort_by(col, not descending))
if self._stripped_rows:
list_of_items = self.interior.get_children('')
for i in range(len(list_of_items)):
self.interior.tag_configure(list_of_items[i], background=self._stripped_rows[i%2])
def destroy(self):
self.interior.destroy()
def item_ID(self, index):
return self.interior.get_children()[index]
if __name__ == '__main__':
try:
from Tkinter import Tk
import tkMessageBox as messagebox
except ImportError:
from tkinter import Tk
from tkinter import messagebox
root = Tk()
def on_select(data):
print("called command when row is selected")
print(data)
print("\n")
def show_info(msg):
messagebox.showinfo("Table Data", msg)
mc = Multicolumn_Listbox(root, ["column one","column two", "column three"], stripped_rows = ("white","#f2f2f2"), command=on_select, cell_anchor="center")
mc.interior.pack()
mc.insert_row([1,2,3])
show_info("mc.insert_row([1,2,3])")
mc.row.insert([4,5,7])
show_info("mc.row.insert([4,5,7])")
mc.update_row(0, [7,8,9])
show_info("mc.update_row(0, [4,5,6])")
mc.update([[1,2,3], [4,5,6]])
show_info("mc.update([[1,2,3], [4,5,6]])")
mc.select_row(0)
show_info("mc.select_row(0)")
print("mc.selected_rows")
print(mc.selected_rows)
print("\n")
print("mc.table_data")
print(mc.table_data)
print("\n")
print("mc.row[0]")
print(mc.row[0])
print("\n")
print("mc.row_data(0)")
print(mc.row_data(0))
print("\n")
print("mc.column[1]")
print(mc.column[1])
print("\n")
print("mc[0,1]")
print(mc[0,1])
print("\n")
mc.column[1] = ["item1", "item2"]
mc.update_column(2, [8,9])
show_info("mc.update_column(2, [8,9])")
mc.clear()
show_info("mc.clear()")
mc.table_data = [[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15], [16,17,18], [19,20,21]]
show_info("mc.table_data = [[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15], [16,17,18], [19,20,21]]")
mc.delete_row(1)
show_info("mc.delete_row(1)")
row = mc.row[0].update([2,4,5])
show_info("mc.row[0].update([2,4,5])")
root.mainloop()
|