Saving Datas From Treeview To Csv File Tkinter Python
Hello I wrote a program in tkinter (Python) and I have a problem with saving datas from treeview to file CSV. I dont use any sql database. Just problem with saving content of treev
Solution 1:
Here is minimal working code without not important functions, variables, modules but with some example data at start.
Main element is
for row_id in tree.get_children():
row = tree.item(row_id)['values']
csvwriter.writerow(row)
import tkinter as tk # PEP8: `import *` is not preferred
from tkinter import ttk
import csv
# --- functions ----
def load_csv():
with open("new.csv") as myfile:
csvread = csv.reader(myfile, delimiter=',')
for row in csvread:
print('load row:', row)
tree.insert("", 'end', values=row)
def save_csv():
with open("new.csv", "w", newline='') as myfile:
csvwriter = csv.writer(myfile, delimiter=',')
for row_id in tree.get_children():
row = tree.item(row_id)['values']
print('save row:', row)
csvwriter.writerow(row)
# --- main ---
root = tk.Tk()
tree = ttk.Treeview(root, height=25, selectmode='extended')
tree.pack()
tree["columns"] = ("one", "two", "three", "four")
tree.column("one", width=120)
tree.column("two", width=160)
tree.column("three", width=130)
tree.column("four", width=160)
tree.heading("one", text="Numer seryjny leku")
tree.heading("two", text="Nazwa Leku")
tree.heading("three", text="Ilość ampułek")
tree.heading("four", text="Data ważności")
tree["show"] = "headings"
button_load = tk.Button(root, text='Load', command=load_csv)
button_load.pack()
button_save = tk.Button(root, text='Save', command=save_csv)
button_save.pack()
# add some data for test
tree.insert("", 'end', values=[1, "A", "B", "C"])
tree.insert("", 'end', values=[2, "X", "Y", "Z"])
root.mainloop()
BTW: See PEP 8 -- Style Guide for Python Code
Post a Comment for "Saving Datas From Treeview To Csv File Tkinter Python"