C# 如何在Python控制台应用程序中创建不确定的进度条?
我正在用Python重写一个C#Console应用程序,我想移植一个我编写的基于控制台的不确定进度条类 我有使用文本创建确定进度条的例子,但我不确定如何创建不确定进度条。我想我需要某种线程。谢谢你的帮助 这是一节课:C# 如何在Python控制台应用程序中创建不确定的进度条?,c#,python,progress-bar,C#,Python,Progress Bar,我正在用Python重写一个C#Console应用程序,我想移植一个我编写的基于控制台的不确定进度条类 我有使用文本创建确定进度条的例子,但我不确定如何创建不确定进度条。我想我需要某种线程。谢谢你的帮助 这是一节课: public class Progress { String _status = ""; Thread t = null; public Progress(String status) { _status = status; }
public class Progress {
String _status = "";
Thread t = null;
public Progress(String status) {
_status = status;
}
public Progress Start() {
t = new Thread(() => {
Console.Write(_status + " ");
while (true) {
Thread.Sleep(300);
Console.Write("\r" + _status + " ");
Thread.Sleep(300);
Console.Write("\r" + _status + " . ");
Thread.Sleep(300);
Console.Write("\r" + _status + " .. ");
Thread.Sleep(300);
Console.Write("\r" + _status + " ...");
}
});
t.Start();
return this;
}
public void Stop(Boolean appendLine = false) {
t.Abort();
Console.Write("\r" + _status + " ... ");
if (appendLine)
Console.WriteLine();
}
}
(p.S.请随意学习进度课程)我在Python中实现了类似的功能。请查看此内容,并根据您的需要进行修改:
class ProgressBar(object):
def __init__(self, min_val=0, max_val=100, width=30, stdout=sys.stdout):
self._progress_bar = '[]' # holds the progress bar string
self._old_progress_bar = '[]'
self.min = min_val
self.max = max_val
self.span = max_val - min_val
self.width = width
self.current = 0 # holds current progress value
self.stdout = stdout
self.update(min_val) # builds the progress bar string
def increment(self, incr):
self.update(self.current + incr)
def update(self, val):
"""Rebuild the progress bar string with the given progress
value as reference.
"""
# cap the value at [min, max]
if val < self.min: val = self.min
if val > self.max: val = self.max
self.current = val
# calculate percentage done
diff = self.current - self.min
done = int(round((float(diff) / float(self.span)) * 100.0))
# calculate corresponding number of filled spaces
full = self.width - 2
filled = int(round((done / 100.0) * full))
# build the bar
self._progress_bar = '[%s>%s] %d%%' % \
('=' * (filled - 1), ' ' * (full - filled), done)
def draw(self, padding=0):
"""Draw the progress bar to current line in stdout.
"""
if self._old_progress_bar != self._progress_bar:
self._old_progress_bar = self._progress_bar
self.stdout.write('\r%s%s ' %
(' ' * padding, self._progress_bar))
self.stdout.flush() # force stdout update
def close(self):
"""Finish the progress bar. Append a newline and close
stdout handle.
"""
self.stdout.write('\n')
self.stdout.flush()
def __str__(self):
return self._progress_bar
这将在命令行上执行动画。这里应该有足够的Python线程示例
编辑:
可能的螺纹解决方案;我不知道写一个真正的线程是否会更有效,因为我不太会使用python线程。。
从线程导入计时器
导入系统,时间
def animation ( i = 0 ):
sys.stdout.write( '\r' + ( '.' * i ) + ' ' )
sys.stdout.flush()
Timer( 0.5, animation, ( 0 if i == 3 else i + 1, ) ).start()
animation()
print( 'started!' )
while True:
pass
一般来说,您不应该中止线程。看。谢谢,这很好,但是马车返回似乎不起作用。它跳到下一行嗯。那么Jython可能就是原因;不过我不知道,我只使用了CPython://切换到了Python2.7.1,但仍然不起作用。一切都好。我不会被进度指标挂断。因为我知道它应该适合我,所以我把你的标记为正确。嗯,我只是切换到我的Python2安装(我通常使用Python3)来测试它(运行Python2.7),它在那里也工作了:真奇怪。。
import sys, time
while True:
for i in range( 4 ):
sys.stdout.write( '\r' + ( '.' * i ) + ' ' )
sys.stdout.flush()
time.sleep( 0.5 )
def animation ( i = 0 ):
sys.stdout.write( '\r' + ( '.' * i ) + ' ' )
sys.stdout.flush()
Timer( 0.5, animation, ( 0 if i == 3 else i + 1, ) ).start()
animation()
print( 'started!' )
while True:
pass