我稍微改变了你的代码。现在它运行了!首先是评论版本:
第一行告诉python这个源文件包含utf-8字符
# -*- coding: utf-8 -*-
然后您需要导入您最终将使用的所有内容。你不必像这样在文件顶部这样做,但我是一个 C 人,这就是我喜欢的方式......
import threading
import sys
import time
import os
spinner="▏▎▍▌▋▊▉█▉▊▌▍▎" #utf8
#convert the utf8 spinner string to a list
chars=[c.encode("utf-8") for c in unicode(spinner,"utf-8")]
class spin(threading.Thread): # not sure what to put in the brackets was (threading.Thread, but now im not sure whether to use processes or not)
线程很适合这个
def __init__(self):
super(spin,self).__init__() # dont understand what this does
由于您正在覆盖init使用自己的 init 的 threading.Thread 方法,您需要调用父类的 init 以确保该对象正确启动。
self._stop = False
我将其更改为布尔值。 threading.Event 对此来说太过分了。
def run (self):
pos=0
while not self._stop:
sys.stdout.write("\r"+chars[pos])
sys.stdout.flush()
time.sleep(.15)
pos+=1
pos%=len(chars)
def cursor_visible(self):
os.system("tput cvvis")
def cursor_invisible(self):
os.system("tput civis")
def stop(self):
self._stop = True #the underscore makes this a private variable ?
有点。它实际上并不是私有的,下划线只是告诉每个人访问它是一种不好的形式。
def stopped(self):
return self._stop == True
最后对代码进行一个小测试:
if __name__ == "__main__":
s = spin()
s.cursor_invisible()
s.start()
a = raw_input("")
s.stop()
s.cursor_visible()
这是未注释的版本......
# -*- coding: utf-8 -*-
import threading
import sys
import time
import os
spinner="▏▎▍▌▋▊▉█▉▊▌▍▎" #utf8
#convert the utf8 spinner string to a list
chars=[c.encode("utf-8") for c in unicode(spinner,"utf-8")]
class spin(threading.Thread): # not sure what to put in the brackets was (threading.Thread, but now im not sure whether to use processes or not)
def __init__(self):
super(spin,self).__init__() # dont understand what this does
self._stop = False
def run (self):
pos=0
while not self._stop:
sys.stdout.write("\r"+chars[pos])
sys.stdout.flush()
time.sleep(.15)
pos+=1
pos%=len(chars)
def cursor_visible(self):
os.system("tput cvvis")
def cursor_invisible(self):
os.system("tput civis")
def stop(self):
self._stop = True #the underscore makes this a private variable ?
def stopped(self):
return self._stop == True
if __name__ == "__main__":
s = spin()
s.cursor_invisible()
s.start()
a = raw_input("")
s.stop()
s.cursor_visible()