Python实现win命令行密码输入的效果_PythonWidget[1]
有兴趣的可以玩一玩import msvcrt
def pwd_input():
chars = []
while True:
try:
newChar = msvcrt.getch().decode(encoding="utf-8")
except:
return input("【温馨提醒:当前未在cmd命令行下运行,密码输入无法隐藏】:\n请在文件管理器空白处下按住shift + 右键 打开powershell/cmd 运行 python wenjianmingzi.py")
if newChar in "\r\n":
break
elif newChar == "\b":
if chars:
del chars[-1]
msvcrt.putch("\b".encode(encoding="utf-8"))
msvcrt.putch( " ".encode(encoding="utf-8"))
msvcrt.putch("\b".encode(encoding="utf-8"))
else:
chars.append(newChar)
msvcrt.putch("*".encode(encoding="utf-8"))
return ("".join(chars) )
if __name__ == '__main__':
print("\n密码:"+pwd_input())
页:
[1]