1.11M Members

Selecting treeview item

 
0
 

I'm fairly new to python. I've been using a modified verson of McListBox from a posting by vegaseat to get 2 treeviews going. It's been working well until I've been trying to be able to click on one to select what will be shown in the second.
Anybody have an idea how to read which line is selected from either listbox?

#-------------------------------------------------------------------------------
# Name:        treeListBox_sample

# Created:     05/01/2013

#-------------------------------------------------------------------------------


#   Here the TreeView widget is configured as a multi-column listbox
#   with adjustable column width and column-header-click sorting.
#   Tested with Python 3.3
#   Modified code from vegaseat's McListBox

import tkinter as tk
import tkinter.font as tkFont
import tkinter.ttk as ttk
root = tk.Tk()
root.wm_title("ttk.TreeView as multicolumn ListBox")

class treeListBox(object):
    """use a ttk.TreeView as a multicolumn ListBox"""
    def __init__(self,element_header,element_list):
        self.element_header = element_header
        self.element_list = element_list
        self.tree = None
        self._setup_widgets()
        self._build_tree()
    def _setup_widgets(self):
        s = """\
    click on header to sort by that column
    to change width of column drag boundary
    """
        msg = ttk.Label(wraplength="4i", justify="left", anchor="n",
            padding=(10, 2, 10, 6), text=s)
        msg.pack(fill='x')
        container = ttk.Frame()
        container.pack(fill='both', expand=True)
        # create a treeview with dual scrollbars
        self.tree = ttk.Treeview(columns=self.element_header, show="headings")
        vsb = ttk.Scrollbar(orient="vertical", command=self.tree.yview)
        hsb = ttk.Scrollbar(orient="horizontal", command=self.tree.xview)
        self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
        self.tree.grid(column=0, row=0, sticky='nsew', in_=container)
        vsb.grid(column=1, row=0, sticky='ns', in_=container)
        hsb.grid(column=0, row=1, sticky='ew', in_=container)
        container.grid_columnconfigure(0, weight=1)
        container.grid_rowconfigure(0, weight=1)
    def _build_tree(self):
        for col in self.element_header:
            self.tree.heading(col, text=col.title(),
                command=lambda c=col: sortby(self.tree, c, 0))
            # adjust the column's width to the header string
            self.tree.column(col, width=tkFont.Font().measure(col.title()))
        for item in self.element_list:
            self.tree.insert('', 'end', values=item)
            # adjust column's width if necessary to fit each value
            for ix, val in enumerate(item):
                col_w = tkFont.Font().measure(val)
                if self.tree.column(self.element_header[ix], width=None) < col_w:
                    self.tree.column(self.element_header[ix], width=col_w)
def isnumeric(s):
    """test if a string is numeric"""
    for c in s:
        if c in "1234567890-.":
            numeric = True
        else:
            return False
    return numeric
def change_numeric(data):
    """if the data to be sorted is numeric change to float"""
    new_data = []
    if isnumeric(data[0][0]):
        # change child to a float
        for child, col in data:
            new_data.append((float(child), col))
        return new_data
    return data
def sortby(tree, col, descending):
    """sort tree contents when a column header is clicked on"""
    # grab values to sort
    data = [(tree.set(child, col), child) for child in tree.get_children('')]
    # if the data to be sorted is numeric change to float
    data = change_numeric(data)
    # now sort the data in place
    data.sort(reverse=descending)
    for ix, item in enumerate(data):
        tree.move(item[1], '', ix)
    # switch the heading so that it will sort in the opposite direction
    tree.heading(col,
        command=lambda col=col: sortby(tree, col, int(not descending)))

# the test data ...
car_header = ['car', 'repair']
car_list = [
('Hyundai', 'brakes') ,
('Honda', 'light') ,
('Lexus', 'battery') ,
('Benz', 'wiper') ,
('Ford', 'tire') ,
('Chevy', 'air') ,
('Chrysler', 'piston') ,
('Toyota', 'brake pedal') ,
('BMW', 'seat')
]

element_header = ['symbol', 'name', 'atomic weight', 'melt (K)', 'boil (K)']
element_list = [
('H', 'Hydrogen', '1.00794', '13.81', '20.28') ,
('He', 'Helium', '4.00260', '0.95', '4.216') ,
('Li', 'Lithium', '6.941', '453.7', '1615') ,
('Be', 'Beryllium', '9.01218', '1560', '3243') ,
('B', 'Boron', '10.811', '2365', '4275') ,
('C', 'Carbon', '12.011', '3825', '5100') ,
('N', 'Nitrogen', '14.0067', '63.15', '77.344') ,
('O', 'Oxygen', '15.9994', '54.8', '90.188') ,
('F', 'Fluorine', '18.99840', '53.65', '85.0')
]

element_listbox = treeListBox(element_header,element_list)
car_listbox = treeListBox(car_header,car_list)
print(element_list)
print(car_list)
root.mainloop()
Question Self-Answered as of 1 Year Ago
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article