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()
code
Assistant
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()
There are a few issues that need to be addressed in the code:
init should be __init__ (with two underscores on each side) to properly indicate a constructor method.
In ItemEntry, super().__init__(master) should be added to properly initialize the parent class.
In App, super().__init__(master) should be added to properly initialize the parent class.
In App, the buttons for saving and loading data should be placed in a separate frame to avoid overlapping with the item entries.
In App, the data should be loaded into the dictionary self.items outside of the loop that iterates over the lines in the file, and the item_entry assignment should be moved inside the loop.
Here's the corrected code:
import tkinter as tk
from tkinter import filedialog
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()
code
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 frame for the buttons
button_frame = tk.Frame(self.master)
button_frame.grid(row=len(self.locations) + 1, column=1, pady=10)
# create a button for saving the data to file
save_button = tk.Button(button_frame, text="Save", command=self.save_data)
save_button.pack(side=tk.LEFT, padx=5)
# create a button for loading data from file
load_button = tk.Button(button_frame, text="Load", command=self.load_data)
load_button.pack(side=tk.LEFT, padx=5)
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()