更新时间:2023-12-02 21:35:10
为什么要限制自己使用lambda
?做一个微调器,最容易保持状态:
Why are you limiting yourself to a lambda
? To do a spinner, it's easiest to maintain state:
class Spinner(object):
def __init__(self):
self._chars = r"-\|/-\|/"
self._pos = 0
def spin(self):
sys.stdout.write(self._chars[self._pos])
sys.stdout.write("\r")
sys.stdout.flush()
self._pos += 1
if self._pos >= len(self._chars):
self._pos = 0
return True
spinner = Spinner()
gobject.timeout_add(2 * 1000, spinner.spin)
注意:以上内容未经测试,但应该使总体思路足够清楚.
Note: the above is untested, but should make the general idea clear enough.