当其他线程在 Python 中使用 Tk 完成时,如何立即关闭加载屏幕?
我有一个使用 Tkinter 的 Python GUI。我必须通过 SSH 连接到另一个地方才能获取数据。我启动了一个新线程来执行此操作,这样 GUI 就不会挂起。在此期间,我想弹出一个屏幕,让用户知道它正在加载。一旦程序完成获取数据,我想关闭加载屏幕。我必须做什么才能让我的主循环识别出线程已完成?我尝试使用该线程关闭主循环中存在的加载屏幕,但后来我发现这不起作用。
我见过一些不使用 GUI 的生产者消费者模型,它们有 while 循环。但这对我没有帮助。我也不想下载和安装其他软件包,但导入就可以了。感谢您的帮助!
I have a Python GUI that uses Tkinter. I have to SSH into another place to get data. I start a new thread to do this so that the GUI doesn't hang. During this time, I want to pop up a screen that lets the user know it is loading. Once the program is finished getting the data, I want to close the loading screen. What must I do to have my main loop recognize that the thread is done? I've tried to use that thread to close the loading screen that exists in the main loop, but then I discovered that doesn't work.
I have seen some producer consumer models that don't use GUIs, and they have while loops. This doesn't help me though. I also don't want to download and install other packages, but imports are ok. Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让您的线程在完成时设置一个标志。让 GUI 定期检查该标志并在设置后关闭该窗口。
您可以通过创建一个检查该标志的函数来检查该标志,如果未设置它,则使用
after
使其自身在几百毫秒后再次运行。线程退出后窗口不会立即消失,但只要延迟不超过几百毫秒,用户就永远不会注意到。Have your thread set a flag when it is done. Have the GUI periodically check for that flag and dismiss the window when it is set.
You can check for the flag by creating a function that checks for the flag, and if it's not set it uses
after
to have itself run again a few hundred ms later. The window won't go away immediately after the thread exits, but as long as the lag isn't more than a couple hundred ms the user will never notice.