2

When the close button is pressed, the window is frozen for a few seconds. In the process I use threading to call a main function(selenium).

In addition, I used window.update() but the result is the same

How to make the window flow smooth when the push button?

from tkinter import *
import threading
import time
from selenium import webdriver

gdriver=None

class Task(threading.Thread):

     def __init__(self,name,namefunc):
         threading.Thread.__init__(self,target=namefunc,name=name,daemon=True)

         threads=list(filter(lambda x: type(x) is Task,threading.enumerate()))

         if len(threads) == 0:
             self.start()
             print("{} Running...".format(self.getName()))
         else:
             print("Wait...")
             label.configure(text="Thread exist!")



def processStart():
    global gdriver
    label.configure(text="Process ON")
    driver=webdriver.Chrome()
    gdriver=driver
    driver.get("https://www.google.com")
    time.sleep(5)
    driver.quit()
    print("Done...")


def close_app():
    threads=[x for x in list(filter(lambda x : x.is_alive() and type(x) is Task,threading.enumerate()))]
    print("Threads: {}".format(threads))
    if len(threads) > 0:
      while True:
        if gdriver:
            print("Close browser....")
            gdriver.quit()
            break
    else:
        if gdriver:
            gdriver.quit()
    print("Close window...")
    window.destroy()
    sys.exit()


if __name__=='__main__':

    window=Tk()
    window.title("App Sample")
    window.geometry("300x200")
    label =Label(window, text="Process OFF")
    label.pack(fill = X)
    button=Button(window,text="Start", command=lambda : Task("Thread 1", processStart))
    buttonQuit=Button(window, text="Close", command=close_app)
    button.pack(fill=X)
    buttonQuit.pack(fill=X)
    window.mainloop()
Mican
  • 73
  • 5
  • ***"Quit button"***: There is not such a `Button`, do you mean `"Close"` `Button`? ***"frozen for a few seconds"***: Yes, read [While Loop Locks Application](https://stackoverflow.com/questions/28639228/python-while-loop-locks-application) – stovfl Oct 23 '19 at 11:18

0 Answers0