Using the mouse component with 'end routine on press' 'valid click' causes TypeError: can't pickle weakref objects

In the data file, you can only save simple text or numeric data (i.e. it is effectively a text file). So you can’t meaningfully embed other sorts of objects (like pictures or sounds or other complex Python objects).

So in your code above, thisExp.addData('basketchosen', basketchosen) is probably OK, as basketchosen should be a simple text value (either 'left' or 'right'). So the problem is presumably thisExp.addData('movingPiece', movingPiece).

It’s not clear from the code above what movingPiece is, but because you check whether it is None, I’m guessing it is some kind of object, such as a stimulus component. An object can be turned into some sort of representation which can be stored in a file on disk (‘pickling’ it), but tabular data files aren’t really suitable for that.

You should be able to get around this by just saving the name of the object instead, e.g:

thisExp.addData('movingPiece', movingPiece.name)

although that will cause an error if movingPiece is None. I’m not sure how your movePicked() function works in terms of what it returns: you might require some additional checking to handle that possibility before the .addData() step.