Created
November 30, 2023 03:49
-
-
Save hdary85/059aa659fdc28aab4303b5d5d812083f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Wed Nov 29 15:02:06 2023 | |
@author: Lenovo | |
""" | |
nested_dict = { | |
'rule1': { | |
'name': 'xy1z11111', | |
'Reccurence': { | |
'IND': OrderedDict({ | |
3:5, | |
5:10, | |
9:20}) , | |
'ENT': OrderedDict({ | |
500:5, | |
1000:10, | |
2000:20}) | |
}, | |
'Montant': { | |
'IND': OrderedDict({ | |
3:5, | |
5:10, | |
9:20}), | |
'ENT': OrderedDict({ | |
500:5, | |
1000:10, | |
2000:20}) | |
} | |
}, | |
'rule2': { | |
'name': 'xy12222', | |
'Reccurence': { | |
'IND': OrderedDict({ | |
3:5, | |
5:10, | |
9:20}) , | |
'ENT': OrderedDict({ | |
500:5, | |
1000:10, | |
2000:20}) | |
}, | |
'Montant': { | |
'IND': OrderedDict({ | |
3:5, | |
5:10, | |
9:20}), | |
'ENT': OrderedDict({ | |
500:5, | |
1000:10, | |
2000:20}) | |
} | |
}, | |
} | |
def populate_tree(tree, item, dictionary): | |
# Clear the tree before repopulating it | |
for child in tree.get_children(item): | |
tree.delete(child) | |
if isinstance(dictionary, dict): | |
for key, value in dictionary.items(): | |
if isinstance(value, dict): | |
sub_item = tree.insert(item, "end", text=key) | |
populate_tree(tree, sub_item, value) | |
else: | |
tree.insert(item, "end", text=key, values=(value,)) | |
else: | |
tree.insert(item, "end", values=(dictionary,)) | |
def on_double_click(event): | |
item = tree.focus() | |
item_text = tree.item(item, "text") | |
old_value = tree.item(item, "values")[0] if tree.item(item, "values") else "" | |
entry_popup = tk.Toplevel(root) | |
entry_popup.title("Edit Key/Value") | |
entry_label = ttk.Label(entry_popup, text=f"Edit {item_text}:") | |
entry_label.pack() | |
entry_key_var = tk.StringVar(value=item_text) | |
entry_key_label = ttk.Label(entry_popup, text="New Key:") | |
entry_key_label.pack() | |
entry_key = ttk.Entry(entry_popup, textvariable=entry_key_var) | |
entry_key.pack() | |
entry_value_var = tk.StringVar(value=old_value) | |
entry_value_label = ttk.Label(entry_popup, text="New Value:") | |
entry_value_label.pack() | |
entry_value = ttk.Entry(entry_popup, textvariable=entry_value_var) | |
entry_value.pack() | |
def save_changes(): | |
new_key = entry_key_var.get() | |
new_value = entry_value_var.get() | |
parent_item = tree.parent(item) | |
tree.move(item, parent_item, tree.index(item)) | |
tree.item(item, text=new_key, values=(new_value,)) | |
update_dictionary(item_text, new_key, new_value) | |
entry_popup.destroy() | |
save_button = ttk.Button(entry_popup, text="Save", command=save_changes) | |
save_button.pack() | |
def update_dictionary(old_key, new_key, new_value): | |
# Update the dictionary based on the edited key or value | |
# This function should update the actual dictionary | |
def find_and_replace(d, old_key, new_key, new_value): | |
for k, v in d.items(): | |
if k == old_key: | |
d[new_key] = d.pop(old_key) | |
if isinstance(v, dict): | |
d[new_key] = {} | |
for sub_k, sub_v in v.items(): | |
d[new_key][sub_k] = sub_v | |
elif isinstance(v, dict): | |
find_and_replace(v, old_key, new_key, new_value) | |
elif v == old_key: | |
d[k] = new_value | |
find_and_replace(nested_dict, old_key, new_key, new_value) | |
def delete_key(): | |
item = tree.focus() | |
item_text = tree.item(item, "text") | |
delete_key_recursive(nested_dict, item_text) | |
tree.delete(item) | |
def delete_key_recursive(dictionary, key): | |
for k, v in dictionary.items(): | |
if k == key: | |
del dictionary[k] | |
return | |
elif isinstance(v, dict): | |
delete_key_recursive(v, key) | |
def add_key_value(): | |
parent_item = tree.focus() | |
parent_text = tree.item(parent_item, "text") if parent_item else "" | |
add_popup = tk.Toplevel(root) | |
add_popup.title("Add New Key/Value") | |
add_label = ttk.Label(add_popup, text="Add New Key/Value:") | |
add_label.pack() | |
add_key_var = tk.StringVar() | |
add_key_label = ttk.Label(add_popup, text="New Key:") | |
add_key_label.pack() | |
add_key = ttk.Entry(add_popup, textvariable=add_key_var) | |
add_key.pack() | |
add_value_var = tk.StringVar() | |
add_value_label = ttk.Label(add_popup, text="Value:") | |
add_value_label.pack() | |
add_value = ttk.Entry(add_popup, textvariable=add_value_var) | |
add_value.pack() | |
def save_new_key_value(): | |
new_key = add_key_var.get() | |
new_value = add_value_var.get() | |
if parent_item == "": | |
nested_dict[new_key] = new_value | |
else: | |
parent_dict = find_parent_dictionary(nested_dict, parent_text) | |
if parent_dict is not None: | |
parent_dict[new_key] = new_value | |
populate_tree(tree, "", nested_dict) | |
add_popup.destroy() | |
save_button = ttk.Button(add_popup, text="Save", command=save_new_key_value) | |
save_button.pack() | |
def save_new_key_value(): | |
new_key = add_key_var.get() | |
new_value = add_value_var.get() | |
parent_item = tree.focus() | |
parent_text = tree.item(parent_item, "text") if parent_item else "" | |
if parent_item == "": | |
nested_dict[new_key] = new_value | |
else: | |
parent_dict = find_parent_dictionary(nested_dict, parent_text) | |
if parent_dict is not None: | |
parent_dict[new_key] = new_value | |
populate_tree(tree, "", nested_dict) | |
add_popup.destroy() | |
def find_parent_dictionary(dictionary, key): | |
for k, v in dictionary.items(): | |
if isinstance(v, dict): | |
if k == key: | |
return dictionary[k] | |
else: | |
found = find_parent_dictionary(v, key) | |
if found is not None: | |
return found | |
def create_gui(): | |
global root | |
root = tk.Tk() | |
root.title("Nested Dictionary Editor") | |
main_frame = ttk.Frame(root) | |
main_frame.pack(fill=tk.BOTH, expand=True) | |
global tree | |
tree = ttk.Treeview(main_frame) | |
tree["columns"] = ("Value") | |
tree.column("#0", width=150) | |
tree.column("Value", width=150) | |
tree.heading("#0", text="Key") | |
tree.heading("Value", text="Value") | |
populate_tree(tree, "", nested_dict) | |
tree.bind("<Double-1>", on_double_click) | |
tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) | |
add_button = ttk.Button(main_frame, text="Add Key/Value", command=add_key_value) | |
add_button.pack() | |
delete_button = ttk.Button(main_frame, text="Delete Key", command=delete_key) | |
delete_button.pack() | |
root.mainloop() | |
if __name__ == "__main__": | |
create_gui() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment