如何编码加载屏幕

时间:2016-01-25 06:52:06

标签: python python-3.x loading

程序员如何编写正确的加载屏幕? 这就是我设想加载屏幕背后的代码的方式,但我想知道是否有一种更好的方法,而不仅仅是一堆条件语句:

loading = 0
def load(percent):
    while percent <= 100:
        if percent == 0:
            foo.loadthing() # Just an example function
            percent += 10
        else if percent == 10:
            foo.loadthing()
            percent += 10

        ...

        else if percent == 100:
            foo.loadthing()
load(loading);

2 个答案:

答案 0 :(得分:1)

我认为你让它落后了。我不会根据加载屏幕编写加载屏幕。

不确定您的语言我希望您能理解我的c#prototype

//Gui thread
var screen = LoadingScreen.Show();
await doWork(); //<- c# way to make doWork happen on 2nd thread and continue after the thread has been finished.
screen.Hide();

//somewhere else
async void doWork() {
   for(int i = 0; i < filesToLoad; ++i) {
      LoadFile(i);
      LoadingScreen.SetPercentage = 100.0 * i / filesToLoad;
   }
}

你在这里看到的是2个线程。 1个线程(gui),显示负载屏幕,只要第二个线程正在工作。第二个帖子会向加载屏幕发送更新,说'嘿我做了一些工作,请更新'

更新啊现在我看到你正在使用python。我的想法应该仍然有效。你应该循环完成你需要做的工作,然后从那里计算更新。

答案 1 :(得分:-1)

如何使用sys模块创建加载屏幕: 它可能看起来不像,但这绝对有效..

import sys
import time
from time import sleep
for i in range(101):
    sys.stdout.write('\r')
    sys.stdout.write("[%-10s] %d%%" % ('='*i, 1*i))
    sys.stdout.flush()
    sleep(0.02)
相关问题