In Tkinter, I'm trying to place key/value pairs from a dictionary called 'headers' inside the set() pairs in the set_values tuple below.
Before this process I open a json file, deserialize the data into a dictionary called headers
. This dictionary is for API headers in the Tkinter App.
The set_value
pairs are 5 pairs of entries for API header keys and values. So, if the dict headers only ends up being 3 key/value pairs, I don't want to use all 5 set_value's, I'd only want to use 3.
Basically I'm thinking of a way to place the headers key/value pairs inside the .set() respected number of .set() pairs. See bottom for expected output.
I have a dict() named headers:
headers = {'First': variable1, 'Second': variable2, 'Third': variable3}
And, on Tkinter, I have about 5 different pairs of Entries
(below): I put these .sets()
inside a tuple because I'm thinking it may be easier to iterate & set key/value pairs from headers into set_values below. Or a dict may work better.
set_values = ( ( 1_key_entry.set(), 1_value_entry.set() ),
( 2_key_entry.set(), 2_value_entry.set() ),
( 3_key_entry.set(), 3_value_entry.set() ),
( 4_key_entry.set(), 4_value_entry.set() ),
( 5_key_entry.set(), 5_value_entry.set() ),
)
Now, based on the length of headers
, say 3, I only want those 3 key/value pairs from headers to be inserted inside the .set()'s in set_values
.
My desired output would be: (Notice the .set(key)
and .set(value)
):
set_values = ( ( 1_key_entry.set('First'), 1_value_entry.set(variable1) ),
( 2_key_entry.set('Second'), 2_value_entry.set(variable2) ),
( 3_key_entry.set('Third'), 3_value_entry.set(variable3) )
)
Lengths:
x = len(headers) # Length of dict headers
y = set_values[:x] # set_values pairs needed based on length of headers.
In my App, I have an easy work around but it is based off asserting k is equal to a user input like Content-Type, Authorization etc in the entry field.
for k, v in json_file.items():
if k == 'Content-Type':
1_key_entry.set(k)
1_value_entry.set(v)
elif k == 'Authorization':
2_key_entry.set(k)
2_value_entry.set(v)
.
.
.
However, what I need is that any k/v pair in the headers dictionary can be automatically set to a set() pair in set_values.
CodePudding user response:
I'm not 100% clear on what you're after, but if I understand correctly: you want to split up key/value pairs from a JSON dictionary into pairs of tkinter Entry
widgets?
If that's the case, then here is an example of how to do that in a loop:
import tkinter as tk
root = tk.Tk()
# get your values however you need to, I'm just using numbers as placeholders
headers = {
'First': 1,
'Second': 2,
'Third': 3,
}
# notice how Entry is only being instantiated twice in this loop...
for i, (k, v) in enumerate(headers.items()):
# column of Entry widgets w/ text from dict keys
ent_k = tk.Entry(root)
ent_k.insert(tk.END, k)
ent_k.grid(i, 0)
# column of Entry widgets w/ text from dict values
ent_v = tk.Entry(root)
ent_v.insert(tk.END, v)
ent_v.grid(i, 1)
root.mainloop()
Does that help?
Note that the tricky part of declaring widgets in a loop like this is that accessing the individual entries can't be done by name, so setting their values programmatically becomes difficult. You may have to resort to some kind of trickery involving root.winfo_children()
to get a list of the widgets in the parent window...