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():
    test_scraping=tk.Toplevel()
    test_scraping.title("aaaa")
    test_scraping.geometry("1100x900")
    test_scraping.configure(bg='#282828')


    # 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:
            scraping_nome_campionati_e_tor()
            scraping_nome_squadre_e_tor()
            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:
            message1.configure(text=msg1)
            message1.configure(foreground="red")
            vuoto_elenco_campionati.config(image=render7)
        
        else:
            vuoto_elenco_campionati.config(image=render8)
            message1.configure(foreground="green")


        msg2 = Scraping_Nome_Squadre_MIO.scraping_nome_squadre_e_tor()
        if msg2:
            message2.configure(text=msg2)
            message2.configure(foreground="red")
            vuoto_elenco_squadre.config(image=render7)

        else:
            vuoto_elenco_squadre.config(image=render8)
            message2.configure(foreground="green")


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


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


Read more here: https://stackoverflow.com/questions/68463611/view-in-textbox-everything-that-happens-in-the-python-console-contextlib

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: