Python Forum

Full Version: Need help to display data from a file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm new to Python and trying to code my first little app, I have difficulties reading data from a file.
The goal of the app is that the user enter data corresponding to a person in a GUI (Last Name, First Name, Rank and Service). These data are stored as a list in a json file.
I would like then the data to be displayed in a treeview, each item of the list in its own column, and that's where I'm stuck.
Here's my code:
If you have advices regarding the code or the way to store (json? sqlite? list? dict?...) they're more than welcome Smile

from tkinter import *
from tkinter import ttk
import json

window=Tk()
window.title("Add Record")
window.geometry("720x500")
tree=ttk.Treeview(window, columns=("Last Name", "First Name", "Rank", "Service"))
tree.grid(column=0, row=4, columnspan=4, pady=5)
tree.column("#0", width=0, minwidth=0)
tree.column("#1", width=200)
tree.column("#2", width=200)
tree.column("#3", width=80)
tree.column("#4", width=150)
tree.heading("#1",text="Last Name")
tree.heading("#2",text="First Name")
tree.heading("#3",text="Rank")
tree.heading("#4",text="Service")

def Quit():
    window.destroy()
    
def AddRecord():
    CRUMember=[txtLastName.get(), txtFirstName.get(), txtRank.get(), txtService.get()]
    with open("CRUMembers.json", "a") as file:
        json.dump(CRUMember, file)
        file.write("\n")
        txtLastName.delete(0, END)
        txtFirstName.delete(0, END)
        txtRank.delete(0, END)
        txtService.delete(0, END)
        txtLastName.focus()
        
def DispRecord():
    with open("CRUMembers.json") as file:
        for line in file:
            line=line.strip()
            CRUMember=file.readlines()
            for p in range(len(CRUMember)):
                tree.insert("", "end", values=(CRUMember[p], CRUMember[p+1], CRUMember[p+2], CRUMember[p+3]))

lblLastName=Label(window, text="Last Name", font=("Calibri", "14"))
lblLastName.grid(column=0, row=0, sticky=E, ipadx=5, padx=10, pady=5)
lblFirstName=Label(window, text="First Name", font=("Calibri", "14"))
lblFirstName.grid(column=0, row=1, sticky=E, ipadx=5, padx=10, pady=5)
lblRank=Label(window, text="Rank", font=("Calibri", "14"))
lblRank.grid(column=0, row=2, sticky=E, ipadx=5, padx=10, pady=10)
lblService=Label(window, text="Service", font=("Calibri", "14"))
lblService.grid(column=0, row=3, sticky=E, ipadx=5, padx=10, pady=10)

txtLastName=Entry(window, width=30, font=("Calibri", "14"))
txtLastName.grid(column=1, row=0)
txtFirstName=Entry(window, width=30, font=("Calibri", "14"))
txtFirstName.grid(column=1, row=1)
txtRank=Entry(window, width=30, font=("Calibri", "14"))
txtRank.grid(column=1, row=2)
txtService=Entry(window, width=30, font=("Calibri", "14"))
txtService.grid(column=1, row=3)
txtLastName.focus()

btnAddRecord=Button(window, text="Add Record", command=AddRecord, font=("Calibri", "14"), width=12)
btnAddRecord.grid(column=2, row=0, padx=10, pady=5)
btnDispRecord=Button(window, text="Display Records", command=DispRecord, font=("Calibri", "14"), width=12)
btnDispRecord.grid(column=3, row=0, pady=5, sticky=E)
btnQuit=Button(window, text="Quit", command=Quit, font=("Calibri", "14"))
btnQuit.grid(column=3, row=5, pady=5, sticky=SE)

window.mainloop()
What does "stuck" mean?

Quote:I would like then the data to be displayed in a treeview, each item of the list in its own column, and that's where I'm stuck.

CRUMember is created in the function AddRecord. This means it is local to the function, and is garbage collected when the function exits, unless it is returned. You can use partial to pass an existing (not created in a function) list to the AddRecord function, and since lists are mutable, the changes made will exist outside the function. When you have time, read the Python Style Guide; functions are lower case with underlines, add_record, It makes code posted easier to understand. https://www.python.org/dev/peps/pep-0008/
Sorry about the function names, I didn't knew and I'll read the guide Wink
By stuck, I mean nothing happens when I click on the display_record button.