Tkinter: How to delete items in listbox from a different window Tkinter

27 views Asked by At

I'm still very new to python and this is a practice project of mine I'm creating a task managing app using Tkinter.

I'm working with JSON files if it makes any difference.

so i have a listbox that is populated from my JSON file in this app i can create new tasks and add them to the listbox. the problem is i cant seem top update the listbox once i actualy create a task.

this is the function to populate the lstbox it works fine on every run of the system:

def populate_list():
    listbox.delete(0, END)
    if len(tasks_data) == 0:
        listbox.insert(END, "No tasks please add New Task")
    else:
       for values in tasks_data:
          display_tasks = f"{values}    priority: {tasks_data[values]["priority_lvl"]}    {tasks_data[values]["title"]}"
          listbox.insert(END, display_tasks)

the problem is when i create a new task i generate a new widow in which to do so the creating a new task works fine it creates a JSON entry but after the task has been created and i call on to the function it doesn't seem to do anything:

new_task_window = Tk()
    new_task_window.title("Create New Task")
    new_task_window.geometry("500x500")

    # ---------------
    #    LABELS     #
    # ---------------

    title_label = Label(new_task_window, text="Title: ")
    title_label.place(x=40, y=40)

    priority_label = Label(new_task_window, text="Priority Level: ")
    priority_label.place(x=40, y=65)

    description_label = Label(new_task_window, text="Description: ")
    description_label.place(x=40, y=90)

    # ---------------
    #    ENTRY     #
    # ---------------

    title_entry = Entry(new_task_window)
    title_entry.focus()
    title_entry.place(x=140, y=40, width=300)

    priority_entry = Entry(new_task_window)
    priority_entry.place(x=140, y=65, width=300)

    description_entry = Text(new_task_window)
    description_entry.place(x=140, y=90, width=300, height=300)

    def create_new_task():
        """Gets the lenght of the data in the json file and creates a new task_id based on the length

        Gets the information from the:
        > title_entry
        > priority_entry
        > description_entry
        and create a new_data entry in the form of a dictionary. Save that data to a json file

        If any fields are empty, warning lets the user know
        if the task was created successfully lets the user know and clears all fields ready for new entry
        """

        # takes data in the entry fields for creation of new task
        title = title_entry.get()
        priority_lvl = priority_entry.get()
        description = description_entry.get("1.0", END)

        # gets length of task_data and determines the task_id
        new_id = len(tasks_data) + 1

        # saves a new_task under new_data variable in a dictionary format. uses the arguments
        # for the data
        new_data = {
            int(new_id): {
                "title": title,
                "priority_lvl": priority_lvl,
                "description": description,
            }
        }

        # Checks if any of the fields are empty if they are pops up with a warning message
        if len(title) == 0 or len(priority_lvl) == 0 or len(description) == 0:
            messagebox.showwarning(title="Oops", message="Please dont leave any fields empty!")
        else:
            # if everything is filled out, it tries opening the json file to read if the file does not
            # exist, it creates a new one and saves the new task entry in to the file
            try:
                with open("tasks_data.json", "r") as df:
                    data = json.load(df)

            except FileNotFoundError:
                with open("tasks_data.json", "w") as df:
                    json.dump(new_data, df, indent=4)

            # if file exists it just updates the file with the new data and adds it in to the file
            else:
                data.update(new_data)

                with open("tasks_data.json", "w") as df:
                    json.dump(data, df, indent=4)

            # If everything was successfully lets user know and clears all the entry boxes
            finally:
                populate_list()
                clear_fields(title_entry, priority_entry, description_entry)
                messagebox.showinfo(title="Success", message="Task saved")

    # ---------------
    #    BUTTONS    #
    # ---------------

    # take data from entry fields and create a new task
    create = Button(new_task_window, text="Create", highlightthickness=0, command=create_new_task)
    create.place(x=139, y=400, width=BUTTON_WIDTH)

    # closes the window
    cancel = Button(new_task_window, text="Cancel", highlightthickness=0, command=new_task_window.destroy)
    cancel.place(x=340, y=400, width=BUTTON_WIDTH)

created a populate_list() function and called it to update the list box in a main window but didnt do anything

0

There are 0 answers