def EditItem(product):
print(product)
editbase = Tk()
editbase.title("Edit Item")
editbase.eval('tk::PlaceWindow . center')
main_frame = Frame(editbase)
main_frame.pack(fill=BOTH, expand=1)
my_canvas = Canvas(main_frame)
my_canvas.pack(side=LEFT, fill=BOTH, expand=1)
my_scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
my_canvas.configure(yscrollcommand=my_scrollbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))
second_frame = Frame(my_canvas)
my_canvas.create_window((0, 0), window=second_frame, anchor="nw")
This is where I'm troubled.
text_file = open('Cashier.txt') # I know it's better to use [with open()]
productname = []
counter = 0
for line in text_file:
print(counter)
line = line.strip('\n')
product = line.split("=")
productname.append(product[0])
productprice = product[1]
Button(second_frame, text=productname[counter], width=35, height=2, font=('Arial', 13, 'bold'),
command = lambda: EditItem(productname[counter])).grid(
row=counter, column=0, pady=10, padx=10)
counter += 1
counter = 0
exitbutton = Button(editbase, text="Exit", font=('Arial', 12), width=20, command=editbase.destroy)
exitbutton.pack(pady=10)
editbase.mainloop()
when you open this code there will be rows of product button names for example
Milk
Coffee
Chocolate
I want to make it so that when I press the Coffee Button then the product in the EditItem
will be Coffee and not Milk because the counter after the for
loop will always be 0 and it seems that the button will not pass down its current counter. And so the command will pass EditItem(productname[0])
instead of the EditItem(productname[currentcounter #Example only]
.
I also can't make an individual button because the amount of product is dynamic.