如何使用螺纹在运行命令时阻止tkinter冻结

发布于 2025-02-12 09:47:12 字数 1018 浏览 0 评论 0原文

我正在使用TKINTER制作一个非常大的GUI,并且我有一个按钮来启动Ngrok服务器。它的工作方式是

    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

该按钮的简单代码。 (请记住所有这些功能都在课堂中,因此命令是主。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)

,但问题是当我单击按钮时,命令虽然gui冻结了并且无法正常工作。 Windows最终将其关闭,因为它不会响应。有人知道我如何简单地修复此问题或使用线程吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文