def modify_formula(self): index = self.listbox.curselection() if len(index) != 1: showerror('Error', 'Select one item') else: win = Toplevel(self) win.title('Modify formula') win.geometry('+100+100') itemlabel = self.listbox.get(index) products = Formula(itemlabel).get_formula(parent=win) defaultnumproducts = 5 new = [] for i in range(defaultnumproducts): if products: var1 = makeFormRow(win, label='Enter the name of the product %d' % (i+1), input=products[0][0]) var2 = makeFormRow(win, label='Enter the name of the quantity', input=products[0][1]) new.append((var1, var2)) products.pop(0) else: var1 = makeFormRow(win, label='Enter the name of the product %d' % (i+1)) var2 = makeFormRow(win, label='Enter the name of the quantity') new.append((var1, var2)) Button(win, text='OK', command=lambda: Formula(itemlabel).modify_formula(new, parent=win)).pack(side=LEFT, fill=X, expand=YES) Button(win, text='Cancel', command=win.destroy).pack(side=RIGHT, fill=X, expand=YES) win.bind('<Return>', lambda event: Formula(itemlabel).modify_formula(new, parent=win))