Created
November 30, 2023 04:26
-
-
Save hdary85/f7fee00a04db2205a7bdfb0c4cf78306 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import tkinter as tk | |
from tkinter import simpledialog, messagebox | |
class NestedDictManager: | |
def __init__(self, root, data): | |
self.root = root | |
self.root.title("Nested Dictionary Manager") | |
self.data = data # Use the provided dictionary | |
self.tree = tk.ttk.Treeview(self.root) | |
self.tree.pack(expand=tk.YES, fill=tk.BOTH) | |
self.tree["columns"] = ("value") | |
self.tree.column("#0", width=150, minwidth=25) | |
self.tree.column("value", width=150, minwidth=25) | |
self.tree.heading("#0", text="Key") | |
self.tree.heading("value", text="Value") | |
self.display_dict(self.data) | |
self.btn_frame = tk.Frame(self.root) | |
self.btn_frame.pack(pady=10) | |
self.update_btn = tk.Button(self.btn_frame, text="Update", command=self.update_key) | |
self.update_btn.pack(side=tk.LEFT, padx=5) | |
self.delete_btn = tk.Button(self.btn_frame, text="Delete", command=self.delete_key) | |
self.delete_btn.pack(side=tk.LEFT, padx=5) | |
self.rename_btn = tk.Button(self.btn_frame, text="Rename", command=self.rename_key) | |
self.rename_btn.pack(side=tk.LEFT, padx=5) | |
self.new_key_btn = tk.Button(self.btn_frame, text="New Key", command=self.add_new_key) | |
self.new_key_btn.pack(side=tk.LEFT, padx=5) | |
def display_dict(self, dictionary, parent=""): | |
for key, value in dictionary.items(): | |
if isinstance(value, dict): | |
item = self.tree.insert(parent, "end", text=key) | |
self.display_dict(value, parent=item) | |
else: | |
self.tree.insert(parent, "end", text=key, values=(value,)) | |
def update_key(self): | |
# ... (same as before) | |
def delete_key(self): | |
# ... (same as before) | |
def rename_key(self): | |
# ... (same as before) | |
def add_new_key(self): | |
selected_item = self.tree.selection() | |
if not selected_item: | |
messagebox.showwarning("Warning", "Please select a parent key to add a new key.") | |
return | |
parent_key = self.tree.item(selected_item)["text"] | |
new_key = simpledialog.askstring("New Key", f"Enter a new key for '{parent_key}':") | |
if new_key is not None: | |
self.tree.insert(selected_item, "end", text=new_key) | |
self.add_dict_key(parent_key, new_key) | |
def add_dict_key(self, parent_key, new_key, dictionary=None): | |
if dictionary is None: | |
dictionary = self.data | |
for k, v in dictionary.items(): | |
if k == parent_key: | |
dictionary[k][new_key] = "New Value" # You may modify this line to set a default value | |
self.display_dict(self.data) # Update the display after modifying the dictionary | |
return | |
elif isinstance(v, dict): | |
self.add_dict_key(parent_key, new_key, v) | |
if __name__ == "__main__": | |
data = { | |
'key1': 'value1', | |
'key2': { | |
'key3': 'value3', | |
'key4': 'value4' | |
}, | |
'key5': { | |
'key6': { | |
'key7': 'value7' | |
} | |
} | |
} | |
root = tk.Tk() | |
app = NestedDictManager(root, data) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment