python Tkinter中的简单加载屏幕 [英] Simple loading screen in python Tkinter

查看:52
本文介绍了python Tkinter中的简单加载屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 初学者,尤其是 tkinter.

I am a beginner python learner, tkinter in particular.

我想制作一个简单的 python 脚本的加载屏幕",并在脚本结束后关闭.

I want to make a 'loading screen' of a simple python script and closes after the script ends.

但是制作一个窗口需要一个 mainloop 函数,这意味着它将无限循环或等待用户交互(或者我认为),并且它将消除加载"屏幕的想法.

But making a window requires a mainloop function which means that it will loops infinitely or wait for a user interaction(or so i think) and it will eliminate the idea of a 'loading' screen.

我尝试了一些东西,但结果是 (Put Loading Screen) -> (Loading screen 仍然有 mainloop) -> (Can't Run Script 因为等待)

I tried something but ends up with (Put Loading Screen) -> (Loading screen still have mainloop) -> (Can't Run Script because of waiting)

我想要的细节是(放置加载脚本)->(运行脚本)->(脚本结束)->(加载屏幕销毁)

What i wanted in detail was (Put Loading Script) -> (Run Script) -> (Script ends) -> (Loading Screen destroy)

我在其他语言方面有很多经验,尤其是 Java,但 Java 可以只声明一个框架 -> 之后运行其他东西 -> 调用 frame.dispose() 就是这样.对学习者有什么提示或建议吗?

I got a lot of experience in other languages especially Java but java can just declare a frame -> run other things afterwards -> call a frame.dispose() and that's just it. Any tips or suggestions for a learner?

该脚本实际上是连接到数据库的图像处理算法,我不能只是进行定时等待或休眠,因为数据库可以扩展并且可能需要比分配的时间更长的时间.

The script actually is a image processing algorithm that connects to a database and I can't just put a timed wait or sleep since the database can be expanded and it might be take longer than the allocated time.

推荐答案

这些方面的方法可能对您有用.这将创建窗口 root,并定义一个函数 task,该函数将销毁 root 作为它所做的最后一件事.在本例中,task 只休眠两秒钟,但您可以将 sleep 调用替换为您想要运行的任何代码.

Something along these lines might work for you. This creates the window root, and defines a function task which destroys root as the last thing it does. In this example, task just sleeps for two seconds, but you'd replace that sleep call with whatever code you want to run.

您使用root.after(200, task)task 函数放入主循环事件队列.这意味着代码将首先创建 root 窗口,等待 200 毫秒,然后调用 task(),它会休眠两秒钟并销毁窗口.至少对于这个例子,你需要 200 毫秒的延迟,以便主循环有足够的时间在 sleep 调用停止一切之前绘制窗口(数字可能对你来说不同;如果窗口没有正确绘制).

You put the task function into the main loop event queue with root.after(200, task). This means the code will first create the root window, wait 200 milliseconds, then call task(), which sleeps for two seconds and destroys the window. At least for this example you need the 200 millisecond delay so that the main loop has enough time to draw the window before the sleep call halts everything (the number might be different for you; increase it if the window doesn't draw properly).

import tkinter as tk
from time import sleep

def task():
    # The window will stay open until this function call ends.
    sleep(2) # Replace this with the code you want to run
    root.destroy()

root = tk.Tk()
root.title("Example")

label = tk.Label(root, text="Waiting for task to finish.")
label.pack()

root.after(200, task)
root.mainloop()

print("Main loop is now over and we can do other stuff.")

在代码中添加了注释.

这篇关于python Tkinter中的简单加载屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆