Sure, I can help you with that! Here's an example of a GUI app built with TKinter that displays a grid of locations and items, which can be edited and saved. The app also allows you to load and save the data to a file on disk.
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()
This code defines two classes: ItemEntry and App. The ItemEntry class defines a single row for the grid, containing the location and item entries for a single location. The App class is responsible for creating the whole grid of ItemEntry widgets and managing their state.
Running this code should display an empty grid of four rows, each containing a location label and an item entry. You can edit the text in each item entry and then click the "Save" button to save the data to a file. You can also click the "Load" button to load data from a file and populate the grid.
This code should give you a good starting point for building your own app that displays and edits location and item data. Let me know if you have any questions or need any additional help!