Kuro
Kuro

Reputation: 31

How to use threading to stop tkinter from freezing when a command is run

I'm making a pretty large scale GUI with tkinter, and I have a button to start a ngrok server. The way it works is simple

    def start_ngrok():
        ngrok_creds = open("tools/ngrok_creds.txt", "r")
        ngrok_creds_lines = ngrok_creds.readlines()
        base_creds = ngrok_creds_lines[0]
        ngrok_creds.close()
        try:    
          os.system(f"ngrok.exe http --basic-auth {base_creds} file:///Mal_Files") # starts the ngrok server with custom credentials and opens a file to the webserver.
          ngrok_started_q = True
        except:
          print("Error staring ngrok server, try again later.")
          ngrok_started_q = False
          os.kill

code for the button. (keep in mind all these functions are in a class so the command is Main.start_ngrok)

run_ngrok = tkinter.Button(base_frame, text="Start Ngrok",command=Main.start_ngrok,width=20)
base_frame.create_window(80, 20, window=run_ngrok)

but the issue is when I click the button the command works though the GUI freezes and won't work. Windows eventually closes it because it won't respond. Anyone know how I could simply fix this or use threading?

Upvotes: 0

Views: 96

Answers (0)

Related Questions