Search code examples
python-3.xmultithreadingseleniumtkinterthreadpoolexecutor

concurrent.futures, selenium and tkinter freeze problem


I have a problem which I cannnot understand with these libraries.

My main code looks like this:

import tkinter as tk
import concurrent.futures
from news_bar import NewsBar        
from fbscout import FbScout         #it bases on sellenium

def run():
    scout.login(USER_EMAIL, USER_PASS)

    while True:
        news = str(scout.check_news())
        if news:
            news_bar.add_text(news)

if __name__ == "__main__":
root = tk.Tk()
news_bar = NewsBar(root)
scout = FbScout.Scrpper()
scout.set_groups(GROUPS)

with concurrent.futures.ThreadPoolExecutor() as executor:
    gui = executor.submit(news_bar.mainloop())
    sc = executor.submit(scout.run_browser())
    run = executor.submit(run())

The program freezes and do not execute the rest of a code after running a tkinter's gui mainloop.

gui = executor.submit(news_bar.mainloop())

I don't know how to order the program not to wait until infinite loop execute... When I close the tkinter's window it comes the right way further

COMMENT: To sum up. I have two functions news_bar.mainloop() which is waiting for an event and and run() which is infinite loop. And I want them to work simultaneously and independent form each other.


Solution

  • Works! Needed to create a new class which inherited the first main tkinter class and inside of which was placed the selenium base module.

    class FbBar(NewsBar):
    def __init__(self, master):
        NewsBar.__init__(self, master)
        scout = FbScout.Scrpper()
        scout.set_groups(GROUPS)
        self.update()
        t = threading.Thread(target=self.prepare_browser, args=[scout])
        t.start()
    
    @staticmethod
    def prepare_browser(scout):
        scout.run_browser()
        scout.login(USER_EMAIL, USER_PASS)
    
    
    
    root = tk.Tk()
    news_bar = FbBar(root)
    news_bar.mainloop()