How to input data in a listbox in tkinter

This is an update of the code I made some years ago

import tkinter as tk


root = tk.Tk()
root.title("List App")
root.geometry("400x400")

def retrievedata():
    ''' get data stored '''
    global list_data
    list_data = []
    try:
      with open("save.txt", "r", encoding="utf-8") as file:
       for f in file:
        listbox.insert(tk.END, f.strip())
        list_data.append(f.strip())
        print(list_data)
    except:
        pass

def reload_data():
    listbox.delete(0, tk.END)
    for d in list_data:
        listbox.insert(0, d)


def add_item(event=1):
    global list_data
    if content.get() != "":
        listbox.insert(tk.END, content.get())
        list_data.append(content.get())
        content.set("")


def delete():
    global list_data
    listbox.delete(0, tk.END)
    list_data = []


def delete_selected():

    try:
        selected = listbox.get(listbox.curselection())
        listbox.delete(listbox.curselection())
        list_data.pop(list_data.index(selected))
        # reload_data()
        # # listbox.selection_clear(0, END)
        listbox.selection_set(0)
        listbox.activate(0)
        listbox.event_generate("<<ListboxSelect>>")
        print(listbox.curselection())
    except:
        pass



def quit():
 global root
 with open("save.txt", "w", encoding="utf-8") as file:
  for d in list_data:
   file.write(d + "\n")
 root.destroy()

# LISTBOX

content = tk.StringVar()
entry = tk.Entry(root, textvariable=content)
entry.pack()

button = tk.Button(root, text="Add Item", command=add_item)
button.pack()

button_delete = tk.Button(text="Delete", command=delete)
button_delete.pack()

button_delete_selected = tk.Button(text="Delete Selected", command=delete_selected)
button_delete_selected.pack()

listbox = tk.Listbox(root)
listbox.pack()
entry.bind("<Return>", add_item)

bquit = tk.Button(root, text="Quit and save", command=quit)
bquit.pack()

retrievedata()
root.mainloop()

Code restyling

Some code changes to make the code more readable, maybe. Made some classes just to understand where the widgets on the left and on the right are. Now the widgets are into two frames and not just one under the other.

import tkinter as tk



def retrievedata():
    ''' get data stored '''
    global list_data
    list_data = []
    try:
      with open("save.txt", "r", encoding="utf-8") as file:
       for f in file:
        Frame2.listbox.insert(tk.END, f.strip())
        list_data.append(f.strip())
        print(list_data)
    except:
        pass

def reload_data():
    Frame2.listbox.delete(0, tk.END)
    for d in list_data:
        Frame2.listbox.insert(0, d)


def add_item(event=1):
    global list_data
    if Frame2.content.get() != "":
        Frame2.listbox.insert(tk.END, Frame2.content.get())
        list_data.append(Frame2.content.get())
        Frame2.content.set("")

def insert_item(event=1):

    global list_data
    if Frame2.content.get() != "":
        print(Frame2.listbox.curselection())
        pos = Frame2.listbox.curselection()[0] + 1
        Frame2.listbox.insert(pos, Frame2.content.get())
        if pos < len(list_data):
            list_data[pos] = Frame2.content.get()
        else:
            pass
        Frame2.content.set("")


def delete():
    global list_data
    Frame2.listbox.delete(0, tk.END)
    list_data = []


def delete_selected():

    try:
        selected = Frame2.listbox.get(Frame2.listbox.curselection())
        Frame2.listbox.delete(Frame2.listbox.curselection())
        list_data.pop(list_data.index(selected))
        Frame2.listbox.selection_set(0)
        print(Frame2.listbox.curselection())
    except:
        pass


def quit(destroy=1):
    if destroy:
        Window.root.destroy()
    else:
        with open("save.txt", "w", encoding="utf-8") as file:
            for d in list_data:
                file.write(d + "\n")

class Window:
    root = tk.Tk()
    root.title("List App")

class Frame1:
    frame1 = tk.Frame(Window.root)
    frame1.pack(side="left")

    # ADD ITEM
    button = tk.Button(frame1,
        text="Add Item",
        command=add_item)
    button.pack()

    button = tk.Button(frame1,
        text="Insert Item after",
        command=insert_item)
    button.pack()

    button_delete = tk.Button(frame1, text="Delete", command=delete)
    button_delete.pack()

    button_delete_selected = tk.Button(frame1, text="Delete Selected", command=delete_selected)
    button_delete_selected.pack()
    bquit = tk.Button(frame1, text="Save", command=lambda: quit(0))
    bquit.pack()

    bquit = tk.Button(frame1, text="Quit", command=quit)
    bquit.pack()

class Frame2:
    global list_data
    frame2 = tk.Frame(Window.root)
    frame2.pack(side="left", fill="both", expand=1)
    content = tk.StringVar()
    entry = tk.Entry(frame2, textvariable=content,
        bg="yellow")
    entry.pack(fill="both", expand=1)
    entry.focus()
    listbox = tk.Listbox(frame2)
    listbox.pack(fill="both", expand=1)
    entry.bind("<Return>", add_item)


retrievedata()
Window.root.mainloop()

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.