Home > front end >  Functions executing out of order?
Functions executing out of order?

Time:06-07

I have a problem currently that makes no sense to me whatsoever. I am making a program that runs a script on a website using the selenium web driver. This is paired with a GUI I made with Tkinter. In short, I have a "Launch" button in the gui that is set to trigger this function:

def launch_script(browser, url):
        
    clear_widgets()

    import script
    script.initialize(browser, url)

"script" is an external .py file that contains the code to configure and launch the browser, and "clear_widgets" is a function that destroys all widgets on screen.

def clear_widgets():
    for widget in window.winfo_children():
        widget.destroy()

here is the "script.initialize" function:

def initialize(selectedBrowser, url):
    import selenium, os
    from selenium import webdriver

    if os.name=="nt":
        dir=f"{os.path.expanduser('~')}/AppData/Roaming/Gimkit Cheats"
    elif os.name=="posix":
        dir=f"{os.path.expanduser('~')}.config"
    else:
        raise Exception("Unrecognized operating system.")
        SystemExit()

    # get webdriver executable
    driverExecutable=f"{dir}/{selectedBrowser}driver.exe"
    
    # hide Seleneum logging
    opt=webdriver.chrome.options.Options()
    opt.add_experimental_option('excludeSwitches', ['enable-logging'])
    
    # get active webdriver 
    match selectedBrowser:
        case "Chrome":
            driver = webdriver.Chrome(driverExecutable, options=opt)
        case "Firefox":
            driver = webdriver.Firefox(driverExecutable, options=opt)
        case "Edge":
            driver = webdriver.Edge(driverExecutable, options=opt)

    # open Gimkit
    driver.get(url)

The problem that is confusing me, is that the "clear_widgets" function isn't running until after "script.initialize" has fully opened and loaded the chrome page. At first, I considered that it might be starting the clear function, and then not waiting for it to finish, but adding a delay between the two did nothing.

Please leave a comment if you need more information about my program

CodePudding user response:

Nobody has posted an actual answer but I found the solution to my problem - I misunderstood how the mainloop() function works, and how it handles other events. I won't go into detail, but the tkinter method after() is what I needed, because this is designed not to interfere with the mainloop's processing. Here is the code that I ended up using:

def launch_script(browser, url):
        import script
    
        clear_widgets()

        lbl_loading=tk.Label(text="Loading...", font=("Arial", 36))
        lbl_loading.pack()

        def init():
            script.initialize(browser, url)
            lbl_loading.destroy()
            draw_page2()

        window.after(100, func=init)
  • Related