Welcome to the forum!
You might find the duck coding example instructional: Toy educational Python IDE It shows how to integrate your own version of print
to capture the output to a text area in the exec’d code. Ignore all the bits that deal with the canvas, and focus on the setup of the globals for the exec’d code in the run_code
function.