Listbox in Json file

Viewed 399

I have a json file that i save my listbox data in. After i choose items (highlight) from the listbox and save my file then load it, the listbox items are stored in my file and they are printed but they are not highlighted. How can I highlight them so i can deselect and select again if i want to change my item selection?

THE CODE:

import tkinter as tk
import json
from tkinter.filedialog import  askdirectory
root = tk.Tk()
root.title('Intialization')
value = []


def callback(listbox):
    global value
    value = [listbox.get(ratio) for ratio in listbox.curselection()]


def writeToJSONFile(path, fileName, data):
    filePathNameWExt = path + '/' + fileName + '.json'
    with open(filePathNameWExt, 'w') as fp:
        json.dump(data, fp)


def check():
    global value
    data = {}
    path = askdirectory()
    data['items'] = value
    writeToJSONFile(path, 'json', data)


def w():
    window = tk.Toplevel(root)
    window.title('Main')
    global value
    listbox = tk.Listbox(window, activestyle='dotbox', selectmode=tk.MULTIPLE, exportselection=False)
    values = [100, 155, 200, 255, 300, 355, 400]
    for item in values:
        listbox.insert(tk.END, item)
    scrollbar = tk.Scrollbar(window)
    scrollbar.grid(column=0, row=2, sticky='nse', pady=20)
    listbox.bind('<<ListboxSelect>>', func=lambda z: callback(listbox))
    listbox.config(width=13, height=4, yscrollcommand=scrollbar.set)
    listbox.grid(column=0, row=2, pady=20, sticky='ne')
    save_config = tk.Button(window, text="Save Configuration", bg='green', command=lambda: check())
    save_config.grid(column=0, row=3)
    try:
        f = open('json.json', "r")
        j = json.loads(f.read())
        for key, value in j.items():
            print(key, ":", value)
        value = j['items']
        print(j)
    except FileNotFoundError:
        print("No Json File")
    window.grab_set()


load_btn = tk.Button(root, text="Load", command=w)
load_btn.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
root.mainloop()


1 Answers

After you read this json file,you could get the index of values and make those items selected. Try:

import tkinter as tk
import json
from tkinter.filedialog import  askdirectory
root = tk.Tk()
root.title('Intialization')
value = []


def callback(listbox):
    global value
    value = [listbox.get(ratio) for ratio in listbox.curselection()]


def writeToJSONFile(path, fileName, data):
    filePathNameWExt = path + '/' + fileName + '.json'
    with open(filePathNameWExt, 'w') as fp:
        json.dump(data, fp)


def check():
    global value
    data = {}
    path = askdirectory()
    data['items'] = value
    writeToJSONFile(path, 'json', data)


def w():
    window = tk.Toplevel(root)
    window.title('Main')
    global value
    listbox = tk.Listbox(window, activestyle='dotbox', selectmode=tk.MULTIPLE, exportselection=False)
    values = [100, 155, 200, 255, 300, 355, 400]
    for item in values:
        listbox.insert(tk.END, item)
    scrollbar = tk.Scrollbar(window)
    scrollbar.grid(column=0, row=2, sticky='nse', pady=20)
    listbox.bind('<<ListboxSelect>>', func=lambda z: callback(listbox))
    listbox.config(width=13, height=4, yscrollcommand=scrollbar.set)
    listbox.grid(column=0, row=2, pady=20, sticky='ne')
    save_config = tk.Button(window, text="Save Configuration", bg='green', command=lambda: check())
    save_config.grid(column=0, row=3)
    try:
        f = open('json.json', "r")
        j = json.loads(f.read())
        for key, value in j.items():
            print(key, ":", value)
        value = j['items']
        index_list = [values.index(i) for i in value] # get the index
        for index in index_list:
            listbox.selection_set(index) # make it selected
        f.close()
    except FileNotFoundError:
        print("No Json File")
    window.grab_set()


load_btn = tk.Button(root, text="Load", command=w)
load_btn.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
root.mainloop()
Related