python | 实现控制台效果
随便写的
import sys, time
import threading
import os
import msvcrt
class Cmd:
def __init__(self):
self.buff = b'' # buff string
print('''Ready to Start | PYcmd by Mz1
========================================================''')
def flush(self):
sys.stdout.write(('\r>>> ' + self.buff.decode('latin1') + ' '))
sys.stdout.flush()
def log(self, string): # use to print on the screen
sys.stdout.write(f'\r{string}\n')
sys.stdout.flush()
self.flush()
def call(self, string):
self.log(f' > call {string}')
def run(self):
while True:
while True: # read byte
self.flush() # flush the buff
c = msvcrt.getch()
if c == b'\r':
break
elif c == b'\x08': # backspace
self.buff = self.buff[:-1]
else:
self.buff += c
if self.buff != b'':
threading.Thread(target=self.call, args=(self.buff.decode('latin1'),)).start()
self.buff = b''
if __name__ == '__main__':
cmd = Cmd()
cmd.run()
本文来自博客园,作者:Mz1,转载请注明原文链接:https://www.cnblogs.com/Mz1-rc/p/14393057.html
如果有问题可以在下方评论或者email:mzi_mzi@163.com