View in textbox everything that happens in the Python console (Contextlib)

This window in Tkinter starts various scraping py files. More precisely it initiates their scraping functions.

I tried to add the code to view the Python console scraping results inside a window textobox. I think the code entered is correct, but I will have made some mistakes in its positioning. Scraping works fine, but with the code I entered the Python console just doesn't start and nothing prints in the textobox. Before, however, the scraping was performed immediately after clicking on the button

How can I go about printing all the results from the Python console? (print as you go, in live scraping, do not print everything at the end)

I'm not getting any errors, but in the Python console the bar is flashing and the scraping won't start. Previously I had tried to fiddle a bit with the code, but even the two py files were loaded 2 times each (for a total of 4), but in the meantime the scraping was not printed in the textobox

from tkinter import *
from tkinter import ttk
import tkinter as tk
from contextlib import redirect_stdout

from File import Scraping_Nome_Campionati
from File import Scraping_Nome_Squadre_MIO

from File.Scraping_Nome_Campionati import scraping_nome_campionati_e_tor
from File.Scraping_Nome_Squadre_MIO import scraping_nome_squadre_e_tor

def draw_graph():

    # Text widget with file-like object feature
    class TextOut(tk.Text):
        def __init__(self, master, **kw):
            super().__init__(master, **kw)

        # required output function for a file-like object
        def write(self, message):
            self.insert("insert", message)

    def do_scraping():
        # temporarily redirect sys.stdout
        with redirect_stdout(text) as f:
            print("completed") # this shows in the text box as well
        print("done") # this will show in console instead of text box

        #Start the scraping functions of the two py files and search for errors 
        #msg1 and msg2 recall the two scraping py files, because they check for scraping errors by showing the green icon and the red icon
        msg1 = Scraping_Nome_Campionati.scraping_nome_campionati_e_tor()
        if msg1:

        msg2 = Scraping_Nome_Squadre_MIO.scraping_nome_squadre_e_tor()
        if msg2:


    text = TextOut(test_scraping,width=80,height=50,  background="black", foreground="white")
    text.pack(), y=20)

    button = Button(test_scraping, text="Avvia", bg='#e95420', foreground='white', command=do_scraping), y=512)

Read more here:

Content Attribution

This content was originally published by James D. Com at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: