Python Forum
[Tkinter] How to insert data json to treeview tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to insert data json to treeview tkinter?
#8
In your earlier post you said this worked:
f = open ('personal.json', "r")
data = json.loads(f.read())
This code reads the entire file and returns a str object. Then it loads() the str object to create python objects. That works, but why read the file and then convert when you can do it with one call.
python]f = open ('personal.json', "r")
data = json.load(f)
[/python]
The json.load() call reads the dictionary and creates python objects. No need to call read() to get the file contents.

In addition to using json.load(file_obj) instead of json.loads(str_obj), my example used a context manager to automatically close the json file when we were done using it. Your program never closed the file. Leaving files open is a bad practice, and using a context manager makes it really easy to make sure you always close files.
with open('personal.json', 'r') as file:   # opens the file for reading
    data = json.load(file)  # reads the json file and returns python obects
# Automatically closes the file when you exit the code block (de-indent)
Quote:Now I have another question, I have some entries to be able to modify the data obtained from the treeview. These work and when I modify them in the tree they are updated, but how would I call the json in this situation to update the values and not to add them as another object in the list?
You use json.dump(python_object) to write a json file. You cannot dump() a TreeView, so you'll need to collect the TreeView items in Python object that can be dumped. Using something that has the same structure as the object returned by json.load() would be a good idea. Maybe you can use the same object?
Reply


Messages In This Thread
RE: How to insert data json to treeview tkinter? - by deanhystad - Jan-18-2023, 08:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - update/refresh treeview snakes 5 21,291 Dec-02-2023, 07:05 PM
Last Post: aynous19
  [Tkinter] Search data in treeview without search button TomasSanchexx 3 1,702 Aug-12-2023, 03:17 AM
Last Post: deanhystad
  TKinter JSON Key Error Nu2Python 4 1,428 Jan-02-2023, 10:15 PM
Last Post: Nu2Python
  [Tkinter] Different rows colours in treeview tkinter Krissstian 1 1,350 Nov-20-2022, 09:59 PM
Last Post: woooee
  [Tkinter] About Tkinter Treeview.selection_get() usage. water 3 8,520 Feb-12-2022, 02:19 PM
Last Post: water
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 3,463 Oct-20-2021, 12:14 AM
Last Post: CyKlop
  [Tkinter] How to insert 'Checkbutton' to 'Treeview' and associate each item? water 2 13,380 Dec-19-2020, 05:24 PM
Last Post: water
  [Tkinter] acceleration of data output in treeview tkinter Vladimir1984 4 4,280 Nov-21-2020, 03:43 PM
Last Post: Vladimir1984
  tkinter| listbox.insert problem Maryan 3 3,594 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  how to insert image into Text widget Tkinter atlass218 5 10,117 Apr-17-2019, 05:28 AM
Last Post: atlass218

Forum Jump:

User Panel Messages

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