I have threads that use some class's functions , and those functions print alot of stuff , that i want to display on a Text() widget .
So i tried making the window in the class as a class variable and the command : mainloop() seems to stop everything from continuing ....
Is there any solution for that ?
The general idea i want to do : (converting the console to GUI..)
from tkinter import *
root = Tk()
textbox = Text(root)
textbox.pack()
def redirector(inputStr):
textbox.insert(INSERT, inputStr)
sys.stdout.write = redirector
root.mainloop()
the whole code :
import threading
from queue import Queue
from Spider import Spider
from domain import *
from general import *
from tkinter import *
def mmm(answer1,answer2,master): # answer1,answer2 are user inputs from the first GUI that gets info, master is the root so i can close it
master.destroy()
PROJECT_NAME = answer1
HOMEPAGE = answer2
DOMAIN_NAME = get_domain_name(HOMEPAGE)
QUEUE_FILE = PROJECT_NAME + '/queue.txt'
CRAWLED_FILE = PROJECT_NAME + '/crawled.txt'
NUMBER_OF_THREADS = 8
queue = Queue() # thread queue
Spider(PROJECT_NAME, HOMEPAGE, DOMAIN_NAME) # a class where the prints happen and some other functions.
root = Tk()
textbox = Text(root)
textbox.pack()
def redirector(inputStr):
textbox.insert(INSERT, inputStr)
sys.stdout.write = redirector
root.mainloop()
# create threads (will die when exit)
def create_threads():
for x in range(NUMBER_OF_THREADS):
t = threading.Thread(target=work)
t.daemon = True
t.start()
# do the next link in the queue
def work():
while True:
url = queue.get()
Spider.crawl_page(threading.current_thread().name, url)
queue.task_done()
# each link is a new job
def create_jobs():
for link in file_to_set(QUEUE_FILE):
queue.put(link) # put the link in the thread queue
queue.join() # block until all processed
crawl()
# if there are items in the queue, crawl them
def crawl():
queued_links = file_to_set(QUEUE_FILE)
if len(queued_links) > 0:
print(str(len(queued_links)) + ' links in the queue')
create_jobs()
create_threads()
crawl()