Advertisement
here2share

# Tk_multicolumn_listbox.py

Jun 5th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. # Tk_multicolumn_listbox.py
  2.  
  3. '''
  4. Here the TreeView widget is configured as a multi-column listbox
  5. with adjustable column width and column-header-click sorting.
  6. '''
  7.  
  8. try:
  9.     import Tkinter as tk
  10.     import tkFont
  11.     import ttk
  12. except ImportError:  # Python 3
  13.     import tkinter as tk
  14.     import tkinter.font as tkFont
  15.     import tkinter.ttk as ttk
  16.  
  17. class MultiColumnListbox(object):
  18.     """use a ttk.TreeView as a multicolumn ListBox"""
  19.  
  20.     def __init__(self):
  21.         self.tree = None
  22.         self._setup_widgets()
  23.         self._build_tree()
  24.  
  25.     def _setup_widgets(self):
  26.         s = """Click on header to sort by that column.
  27. To change width of column drag boundary.
  28.        """
  29.         msg = ttk.Label(wraplength="4i", justify="left", anchor="n",
  30.             padding=(10, 2, 10, 6), text=s)
  31.         msg.pack(fill='x')
  32.         container = ttk.Frame()
  33.         container.pack(fill='both', expand=True)
  34.         # create a treeview with dual scrollbars
  35.         self.tree = ttk.Treeview(columns=car_header, show="headings")
  36.         vsb = ttk.Scrollbar(orient="vertical",
  37.             command=self.tree.yview)
  38.         hsb = ttk.Scrollbar(orient="horizontal",
  39.             command=self.tree.xview)
  40.         self.tree.configure(yscrollcommand=vsb.set,
  41.             xscrollcommand=hsb.set)
  42.         self.tree.grid(column=0, row=0, sticky='nsew', in_=container)
  43.         vsb.grid(column=1, row=0, sticky='ns', in_=container)
  44.         hsb.grid(column=0, row=1, sticky='ew', in_=container)
  45.         container.grid_columnconfigure(0, weight=1)
  46.         container.grid_rowconfigure(0, weight=1)
  47.  
  48.     def _build_tree(self):
  49.         for col in car_header:
  50.             self.tree.heading(col, text=col.title(),
  51.                 command=lambda c=col: sortby(self.tree, c, 0))
  52.             # adjust the column's width to the header string
  53.             self.tree.column(col,
  54.                 width=tkFont.Font().measure(col.title()))
  55.  
  56.         for item in car_list:
  57.             self.tree.insert('', 'end', values=item)
  58.             # adjust column's width if necessary to fit each value
  59.             for ix, val in enumerate(item):
  60.                 col_w = tkFont.Font().measure(val)
  61.                 if self.tree.column(car_header[ix],width=None)<col_w:
  62.                     self.tree.column(car_header[ix], width=col_w)
  63.  
  64. def sortby(tree, col, descending):
  65.     """sort tree contents when a column header is clicked on"""
  66.     # grab values to sort
  67.     data = [(tree.set(child, col), child) \
  68.         for child in tree.get_children('')]
  69.     # if the data to be sorted is numeric change to float
  70.     # data =  change_numeric(data)
  71.     # now sort the data in place
  72.     data.sort(reverse=descending)
  73.     for ix, item in enumerate(data):
  74.         tree.move(item[1], '', ix)
  75.     # switch the heading so it will sort in the opposite direction
  76.     tree.heading(col, command=lambda col=col: sortby(tree, col, \
  77.         int(not descending)))
  78.  
  79. # test data...
  80.  
  81. car_header = ['car', 'repair']
  82. car_list = [
  83. ('Hyundai', 'brakes') ,
  84. ('Honda', 'light') ,
  85. ('Lexus', 'battery') ,
  86. ('Benz', 'wiper') ,
  87. ('Ford', 'tire') ,
  88. ('Chevy', 'air') ,
  89. ('Chrysler', 'piston') ,
  90. ('Toyota', 'brake pedal') ,
  91. ('BMW', 'seat')
  92. ]
  93.  
  94.  
  95. if __name__ == '__main__':
  96.     root = tk.Tk()
  97.     root.title("Multicolumn Treeview/Listbox")
  98.     listbox = MultiColumnListbox()
  99.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement