用字符屏蔽键盘输入-Python 3.0版



在windows操作系统上,我想用一个字符来屏蔽我的键盘输入,比如Python版本3和更高版本上的星号

到目前为止,

import getpass
pswd = getpass.getpass('Password:')

我可以隐藏密码输入,但我不能为每个字母显示一个字符

我尝试使用msvcrt模块,但一个没有模块名称"msvcrt"的错误不断出现,可能是因为我有Python版本3及更高版本。我也不想使用tKinter。

我想我正是你所需要的。密码设置为变量名"psw"。不过,这似乎只适用于cmd。

import msvcrt
import time
import sys
import os
def getPASS():
    print("Input password")
    list1 = []
    while True:
        char = msvcrt.getch()
        char =str(char)
        char = char[2:-1]
        if char == "\n'" or char == "\r":
            break
    elif char == "\x08":
        del list1[-1]
        os.system("cls")
        print("Input password:")
        sys.stdout.write("*" * len(list1))
        sys.stdout.flush()
        continue
    else:
        list1.append(char)
        sys.stdout.write("*")
        sys.stdout.flush()
print("n")
psw = "".join(list1)
print(psw)
invalid = ' ,:;/?"}]{[-=+!@#$%^&*()|'
for x in psw:
    if x in invalid:
        print("Character %r is not allowed in password" % x)
        getPASS()
    else:
        pass
getPASS()

请给出任何改进,Dan

最新更新