Python Forum
Need help to display data from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help to display data from a file
#1
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()
Reply
#2
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/
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to display <IPython.core.display.HTML object>? pythopen 3 45,695 May-06-2023, 08:14 AM
Last Post: pramod08728
  Store variable data and display sum after 60 seconds the_dude 11 3,365 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  xml file creation from an XML file template and data from an excel file naji_python 1 2,068 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,172 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,079 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  How to display a pdf file arbiel 3 2,403 Apr-05-2020, 07:03 PM
Last Post: arbiel
  Selection and display of data by combobox LagratteCchouette 10 7,414 Mar-02-2020, 06:34 PM
Last Post: ibreeden
  def Display Data Invalid Syntax error Linuxdesire 1 1,835 Oct-11-2019, 05:10 AM
Last Post: stranac
  export file and display cmd prompt same time jacklee26 1 2,001 Jul-24-2019, 05:15 AM
Last Post: Larz60+
  Display 20 records at a time,data structure or loop pythonds 1 2,430 Mar-29-2018, 11:09 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020