-1
我試圖使用ttk.TreeView
以表格方式顯示CSV中的信息片段。代碼完全符合我想要的。我只是 想要更新樹,每次我點擊第一個Curselec_Croq
Listbox
而不是每次點擊時創建一個新的樹。更新列表框/ TreeView,而不是每次都創建一個新列表
我該怎麼做?
import pdb
#pdb.set_trace()
import sys
import csv
sys.version_info
import tkinter as tk
import tkinter.font as tkFont
import tkinter.ttk as ttk
from tkinter import *
def parse_csv(content, delimiter = ';'): ## We use here ";" to parse CSV
## because of the European way of
csv_data = [] ## dealing with excel-csv
for line in content.split('\n'): ## strips spaces also
csv_data.append([x.strip() for x in line.split(delimiter)])
return csv_data
global car_header
global car_list
class McListBox(object):
"""use a ttk.TreeView as a multicolumn ListBox"""
def __init__(self):
self.tree = None
self._setup_widgets()
self._build_tree()
def _setup_widgets(self):
s ='""'
msg = ttk.Label(wraplength="4i", justify="left", anchor="n",
padding=(10, 2, 10, 6))
msg.pack(fill='x')
container = ttk.Frame()
container.pack(fill='both', expand=True)
# create a treeview with dual scrollbars
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))
# adjust the column's width to the header string
self.tree.column(col,
width=tkFont.Font().measure(col.title()))
for item in car_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(car_header[ix],width=None)<col_w:
self.tree.column(car_header[ix], width=col_w)
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 it will sort in the opposite direction
tree.heading(col, command=lambda col=col: sortby(tree, col,
int(not descending)))
Remplissage = parse_csv(
open('Remplissage.csv', 'rU', encoding="ISO-8859-1").read())
root = tk.Tk()
root.wm_title("Visualizer")
def CurSelet_croq(evt):
global car_header
global car_list
car_header = [
'Used Nutrients in the Profession',
'Nutrients used in this PetFood (Source : Official Website)']
car_list=[]
value=str(liste_croq.get(liste_croq.curselection()))
for i in range(0,len(Remplissage)):
if Remplissage[i][0]=="Name":
for j in range(1,len(Remplissage[i])):
if Remplissage[i][j]==value:
for k in range(0,len(Remplissage)-1):
if Remplissage[k][j]!="ND":
car_list.append([Remplissage[k][0],"OK"])
else:
car_list.append([Remplissage[k][0],""])
print(car_list)
mc_listbox = McListBox()
return car_header, car_list
liste_croq = Listbox(root,width=70, height=10)
for i in range(0,len(Remplissage)):
if Remplissage[i][0]=="Name":
for j in range(1,len(Remplissage[i])):
liste_croq.insert(i,Remplissage[i][j])
liste_croq.bind('<<ListboxSelect>>', CurSelet_croq)
liste_croq.pack()
root.mainloop()
到目前爲止,我已經嘗試創建一個新的root
窗口每次和摧毀前一個,但它只是看起來像它。這不是「更新」。
任何help'd不勝感激。
由於馬蒂諾要求在這裏是一個提取Remplissage.csv
Name;Puppy Small Breed For Small Breed Puppies weighing 9 KG (20 LBS) at maturity;Puppy & Junior For puppies between 9 and 25;Puppy Large Breed For Puppies 25;Adult Small Breed For Small Breed Dogs 1 Year and Older;Chicken & Burbank Potato For All Breeds and Life Stages
Methionine;0.6%;0.6%;0.6%
Metabolisable energy (calculated according NRC85);ND;ND;ND
Moisture,Water,Humidity,Moisture (max.);10%;10%;10%
你讀過任何[上樹視圖控件文件(https://docs.python.org /dev/library/tkinter.ttk.html#ttk-treeview)?有添加,刪除和修改項目的文檔化方法。 –
謝謝你的快速回答。其實我在這裏有點迷路。你能更專注於你認爲我應該做什麼嗎?我深入瞭解了你提出的建議,並且我不知道如何使用這些信息來修改我的代碼。EDIT:我可以在創建新建時使用'delete'來刪除樹,但是我不能明白我該怎麼做。事實上,當我離開McListBox類時,我無法訪問'tree'。 – BoobaGump
請在您的問題中添加一些示例CSV數據。 – martineau