0

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?

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
Kuro
  • 11
  • 4
  • You can use **after** methode instead of threading. Check documentation for root.after() methode. You could use pythons threading module as well – Module_art Jul 02 '22 at 21:40
  • Thank you! I'll look into that and see what I can do. – Kuro Jul 03 '22 at 03:09
  • Okay after a little research and working with test code lol I don't know what the base_frame.after() is supposed to call on to stop the freezing. – Kuro Jul 03 '22 at 03:18
  • "os.system" waits until the called process terminates, therefore the whole GUI is unresponsive. Use "subprocess.Popen" or one of the "os.spawn..." methods with mode "os.P_NOWAIT" instead to run ngrok. – Michael Butscher Jul 03 '22 at 05:05
  • You should know that working with threads, tk.after or spawn new processes is a big task. As a beginner I wouldn't recommend it. Try alternatives bash files, split program in multiple programs... whatever but do not start with threads as a beginner. – Module_art Jul 21 '22 at 20:47

0 Answers0