import tkinter as tk
from tkinter import filedialog

class ItemEntry(tk.Frame):
    def init(self, master=None, location="", item="", on_entry_change=None):
        super().init(master)
        self.master = master
        self.location = location
        self.item = item
        self.on_entry_change = on_entry_change
        self.create_widgets()

    def create_widgets(self):
        self.location_label = tk.Label(self, text=self.location, anchor=tk.W, width=10)
        self.item_entry = tk.Entry(self)
        self.item_entry.insert(tk.END, self.item)
        self.item_entry.bind("<FocusOut>", self.on_focus_out)
        self.location_label.pack(side=tk.LEFT, fill=tk.BOTH, padx=5, pady=5)
        self.item_entry.pack(side=tk.LEFT, fill=tk.BOTH, padx=5, pady=5, expand=True)

    def on_focus_out(self, event):
        new_item = self.item_entry.get().strip()
        if new_item != self.item:
            self.item = new_item
        if self.on_entry_change:
            self.on_entry_change(self)

    def reset(self):
        self.item_entry.delete(0, tk.END)
        self.item_entry.insert(tk.END, self.item)


class App(tk.Frame):
    def init(self, master=None):
        super().init(master)
        self.master = master
        self.locations = ["A1", "A2", "A3", "A4"]
        self.items = {location: "" for location in self.locations}
        self.item_entries = []
        self.create_widgets()
    
    def create_widgets(self):
    # create a grid of item entries for each location
        for idx, location in enumerate(self.locations):
            item_entry = ItemEntry(self.master, location=location, item=self.items[location],
            on_entry_change=self.on_item_change)
            item_entry.grid(row=idx, column=1, sticky=tk.NSEW, padx=5, pady=5)
            self.item_entries.append(item_entry)

            # create a button for saving the data to file
            save_button = tk.Button(self.master, text="Save", command=self.save_data)
            save_button.grid(row=len(self.locations) + 1, column=1, pady=10)

            # create a button for loading data from file
            load_button = tk.Button(self.master, text="Load", command=self.load_data)
            load_button.grid(row=len(self.locations) + 2, column=1, pady=10)

    def on_item_change(self, item_entry):
        self.items[item_entry.location] = item_entry.item

    def save_data(self):
        filename = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", ".txt")])
        if not filename:
            return
        with open(filename, "w") as f:
            f.write("\n".join(["{}\t{}".format(location, item) for location, item in self.items.items()]))

    def load_data(self):
        filename = filedialog.askopenfilename(defaultextension=".txt", filetypes=[("Text files", ".txt")])
        if not filename:
            return
        with open(filename, "r") as f:
            for line in f.readlines():
                location, item = line.strip().split("\t")
        if location in self.items:
            self.items[location] = item
            item_entry = next((ie for ie in self.item_entries if ie.location == location), None)
        if item_entry:
            item_entry.reset()


if __name__ == "__main__":
   root = tk.Tk()
   app = App(master=root)
   app.pack()
   root.mainloop()