Answer the question
In order to leave comments, you need to log in
Treeview ttk How to change the values of a single cell or row?
I found a wonderful code that allows you to display an array in Excel - a style table, and even sorting is a bonus. But I just can’t find on the Internet how to edit the content (values) of an already filled table. Better address cell (element). If not possible, at least a line. Thank you!
Spoiler code. Starting up.
There seems to be some description here, but something didn't help me...
https://docs.python.org/3/library/tkinter.ttk.html#id8
import tkinter as tk
import tkinter.font as tkFont
import tkinter.ttk as ttk
class MultiColumnListbox(object):
def __init__(self):
self.tree = None
self._setup_widgets()
self._build_tree()
def _setup_widgets(self):
msg = ttk.Label(wraplength="4i", justify="left", anchor="n", padding=(10, 2, 10, 6), text='Текст')
msg.pack(fill='x')
container = ttk.Frame()
container.pack(fill='both', expand=True)
self.tree = ttk.Treeview(columns=car_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 car_header:
self.tree.heading(col, text=col.title(),
command=lambda c=col: sortby(self.tree, c, 0))
self.tree.column(col,
width=tkFont.Font().measure(col.title()))
for item in car_list:
self.tree.insert('', 'end', values=item)
for ix, val in enumerate(item):
col_w = tkFont.Font().measure(val)
if self.tree.column(car_header[ix],width=None)<col_w:
self.tree.column(car_header[ix], width=col_w)
def sortby(tree, col, descending):
data = [(tree.set(child, col), child) \
for child in tree.get_children('')]
data.sort(reverse=descending)
for ix, item in enumerate(data):
tree.move(item[1], '', ix)
tree.heading(col, command=lambda col=col: sortby(tree, col, \
int(not descending)))
car_header = ['car', 'rep', 'repair']
car_list = [
('Hyundai', 'brakes', 'brakes') ,
('Honda', 'light', 'brakes') ,
('Lexus', 'battery', 'brakes') ,
('Benz', 'wiper', 'brakes') ,
('Ford', 'tire', 'brakes') ,
('Chevy', 'air', 'brakes') ,
('Chrysler', 'piston', 'brakes') ,
('Toyota', 'brake pedal', 'brakes') ,
('BMW', 'seat', 'brakes')
]
if __name__ == '__main__':
root = tk.Tk()
root.title("Multicolumn Treeview/Listbox")
listbox = MultiColumnListbox()
root.mainloop()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question