Python ‘entry’ text make readable only
Question:
I have the next line of code to create entry text inputs in python:
globals()[f"dv_{key}_{subkey}"] = tk.Entry(self.root, width=250)
That text create input texts to enter data as shown in photo:
I want to make only readable then I entered the command as this:
globals()[f"dv_{key}_{subkey}"] = tk.Entry(self.root, width=250, state='disabled')
Of course it does the job so you can’t input text hat is what I want, the problem is the text they have disappear as shown in photo:
How can i make to make readable only but that they show the real values the had before i entered the state='disabled'
?
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:
You can set the "state" of the Entry widget to "readonly" instead of "disabled". This will make the widget read-only, but still display its value.
Here’s an updated code example:
globals()[f"dv_{key}_{subkey}"] = tk.Entry(self.root, width=250, state='readonly')
With this change, the Entry widget will still display its value, but the user won’t be able to edit it.
You have to insert the data before setting the state, otherwise the state of "readonly" or "disabled" will prevent you from inserting the data.
...
for subkey, subvalue in value.items():
...
subtextbox = tk.Entry(self.root, width=50)
subtextbox.insert(0, subvalue)
subtextbox.configure(state="disabled")
...
I have the next line of code to create entry text inputs in python:
globals()[f"dv_{key}_{subkey}"] = tk.Entry(self.root, width=250)
That text create input texts to enter data as shown in photo:
I want to make only readable then I entered the command as this:
globals()[f"dv_{key}_{subkey}"] = tk.Entry(self.root, width=250, state='disabled')
Of course it does the job so you can’t input text hat is what I want, the problem is the text they have disappear as shown in photo:
How can i make to make readable only but that they show the real values the had before i entered the state='disabled'
?
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"
}
]
}
You can set the "state" of the Entry widget to "readonly" instead of "disabled". This will make the widget read-only, but still display its value.
Here’s an updated code example:
globals()[f"dv_{key}_{subkey}"] = tk.Entry(self.root, width=250, state='readonly')
With this change, the Entry widget will still display its value, but the user won’t be able to edit it.
You have to insert the data before setting the state, otherwise the state of "readonly" or "disabled" will prevent you from inserting the data.
...
for subkey, subvalue in value.items():
...
subtextbox = tk.Entry(self.root, width=50)
subtextbox.insert(0, subvalue)
subtextbox.configure(state="disabled")
...