Python code to read from JSON file expand height entry text to see more
Question:
I the next python code to process any JSON file and show the keys and subkeys in the GUI
The problem I have is I want to make maybe expand in height the entry boxes instances to see more because some results are truncated. All the entry text instances are controlled in the code in these type of lines:
globals()[f"dv_{key}_{subkey}"] = tk.Entry(self.root, width=50)
I mention more height to give more visibility because some characters are truncated because if I change the width from 50 to more as the script autodetects any JSON file even the small input fields will expand and is a mess. However, if I could expand the height to 2 or 3 rows more it would be fine.
This photo shows how the characters are truncated to the end and I can’t see them:
Then how can be modified the enter text inputs to make larger in height an that the characters split to another row to have more visibility and avoid the truncated characters?
Here is code to read de JSON file:
import json
import tkinter as tk
class JsonEditor:
def __init__(self, root, filename):
self.root = root
self.root.title("JSON Editor")
self.json_data = {}
self.textboxes = []
self.filename = filename
self.read_file()
self.create_widgets()
def create_widgets(self):
# create menu bar
menubar = tk.Menu(self.root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Save", command=self.save_file)
menubar.add_cascade(label="File", menu=filemenu)
self.root.config(menu=menubar)
# create textboxes for JSON keys and values
row = 0
for key, value in self.json_data.items():
if isinstance(value, dict):
label = tk.Label(self.root, text=key)
label.grid(row=row, column=0)
row += 1
for subkey, subvalue in value.items():
sublabel = tk.Label(self.root, text=subkey)
sublabel.grid(row=row, column=1)
subtextbox = tk.Entry(self.root, width=50)
subtextbox.insert(0, subvalue)
subtextbox.grid(row=row, column=2)
self.textboxes.append((key, subkey, subtextbox))
row += 1
else:
label = tk.Label(self.root, text=key)
label.grid(row=row, column=0)
textbox = tk.Entry(self.root, width=50)
textbox.insert(0, value)
textbox.grid(row=row, column=1)
self.textboxes.append((key, textbox))
row += 1
# create submit button
submit_button = tk.Button(self.root, text="Submit", command=self.save_file)
submit_button.grid(row=row, column=1)
def read_file(self):
with open(self.filename, "r") as f:
self.json_data = json.load(f)
def save_file(self):
for item in self.textboxes:
if len(item) == 2:
key, textbox = item
self.json_data[key] = textbox.get()
elif len(item) == 3:
key, subkey, subtextbox = item
self.json_data[key][subkey] = subtextbox.get()
with open(self.filename, "w") as f:
json.dump(self.json_data, f)
root = tk.Tk()
# set the display resolution
root.geometry("800x600")
# set the JSON file path
filename = "C:/orderprepare.json"
# create the editor GUI
editor = JsonEditor(root, filename)
# start the GUI main loop
root.mainloop()
The script reads any JSON file with any values but anyway I attach and example of JSON file:
{
"ShipTo": {
"CustomerName1": "test test",
"CustomerName2": "test test",
"Phone": "000-000-0000dfswerwer",
"Address1": "test test",
"Address2": "dfewrwer",
"City": "Tazewellddsewrer",
"StateOrProvince": "TNdsfsewr",
"PostalCode": "82823",
"Country": "USA"
},
"BillTo": {
"CustomerName1": "test2 test2",
"CustomerName2": "test2 test2",
"Address1": "test test",
"Address2": "dfewrwer",
"City": "anywhere",
"StateOrProvince": "TNdsfsewr",
"PostalCode": "84747",
"Country": "USA"
},
"ItemList": [
{
"ItemNumber": "itemxxxx111",
"BuyerPartNumber": "partxxx1111",
"Quantity": "7",
"UnitPrice": "809"
}
]
}
Answers:
From https://www.tutorialspoint.com/python/tk_entry.htm
:
The Entry widget is used to accept single-line text strings from a user.
If you want to display multiple lines of text that can be edited, then you should use the Text widget.
If you want to display one or more lines of text that cannot be modified by the user, then you should use the Label widget.
So if you want to have the user edit the values, you need a text widget and a label widget otherwise.
I the next python code to process any JSON file and show the keys and subkeys in the GUI
The problem I have is I want to make maybe expand in height the entry boxes instances to see more because some results are truncated. All the entry text instances are controlled in the code in these type of lines:
globals()[f"dv_{key}_{subkey}"] = tk.Entry(self.root, width=50)
I mention more height to give more visibility because some characters are truncated because if I change the width from 50 to more as the script autodetects any JSON file even the small input fields will expand and is a mess. However, if I could expand the height to 2 or 3 rows more it would be fine.
This photo shows how the characters are truncated to the end and I can’t see them:
Then how can be modified the enter text inputs to make larger in height an that the characters split to another row to have more visibility and avoid the truncated characters?
Here is code to read de JSON file:
import json
import tkinter as tk
class JsonEditor:
def __init__(self, root, filename):
self.root = root
self.root.title("JSON Editor")
self.json_data = {}
self.textboxes = []
self.filename = filename
self.read_file()
self.create_widgets()
def create_widgets(self):
# create menu bar
menubar = tk.Menu(self.root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Save", command=self.save_file)
menubar.add_cascade(label="File", menu=filemenu)
self.root.config(menu=menubar)
# create textboxes for JSON keys and values
row = 0
for key, value in self.json_data.items():
if isinstance(value, dict):
label = tk.Label(self.root, text=key)
label.grid(row=row, column=0)
row += 1
for subkey, subvalue in value.items():
sublabel = tk.Label(self.root, text=subkey)
sublabel.grid(row=row, column=1)
subtextbox = tk.Entry(self.root, width=50)
subtextbox.insert(0, subvalue)
subtextbox.grid(row=row, column=2)
self.textboxes.append((key, subkey, subtextbox))
row += 1
else:
label = tk.Label(self.root, text=key)
label.grid(row=row, column=0)
textbox = tk.Entry(self.root, width=50)
textbox.insert(0, value)
textbox.grid(row=row, column=1)
self.textboxes.append((key, textbox))
row += 1
# create submit button
submit_button = tk.Button(self.root, text="Submit", command=self.save_file)
submit_button.grid(row=row, column=1)
def read_file(self):
with open(self.filename, "r") as f:
self.json_data = json.load(f)
def save_file(self):
for item in self.textboxes:
if len(item) == 2:
key, textbox = item
self.json_data[key] = textbox.get()
elif len(item) == 3:
key, subkey, subtextbox = item
self.json_data[key][subkey] = subtextbox.get()
with open(self.filename, "w") as f:
json.dump(self.json_data, f)
root = tk.Tk()
# set the display resolution
root.geometry("800x600")
# set the JSON file path
filename = "C:/orderprepare.json"
# create the editor GUI
editor = JsonEditor(root, filename)
# start the GUI main loop
root.mainloop()
The script reads any JSON file with any values but anyway I attach and example of JSON file:
{
"ShipTo": {
"CustomerName1": "test test",
"CustomerName2": "test test",
"Phone": "000-000-0000dfswerwer",
"Address1": "test test",
"Address2": "dfewrwer",
"City": "Tazewellddsewrer",
"StateOrProvince": "TNdsfsewr",
"PostalCode": "82823",
"Country": "USA"
},
"BillTo": {
"CustomerName1": "test2 test2",
"CustomerName2": "test2 test2",
"Address1": "test test",
"Address2": "dfewrwer",
"City": "anywhere",
"StateOrProvince": "TNdsfsewr",
"PostalCode": "84747",
"Country": "USA"
},
"ItemList": [
{
"ItemNumber": "itemxxxx111",
"BuyerPartNumber": "partxxx1111",
"Quantity": "7",
"UnitPrice": "809"
}
]
}
From https://www.tutorialspoint.com/python/tk_entry.htm
:
The Entry widget is used to accept single-line text strings from a user.
If you want to display multiple lines of text that can be edited, then you should use the Text widget.
If you want to display one or more lines of text that cannot be modified by the user, then you should use the Label widget.
So if you want to have the user edit the values, you need a text widget and a label widget otherwise.