使用python3.0版本的字符屏蔽键盘输入

2024-09-30 03:22:36 发布

您现在位置:Python中文网/ 问答频道 /正文

听着,没有人正确地回答我的问题,并且重复了我之前的问题,因为我之前的问题不清楚

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

到目前为止我有这个

import getpass

pswd = getpass.getpass('Password:')

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

我试着使用msvcrt模块,但是一个错误说没有模块名'msvcrt'不断弹出pop,可能是因为我有pythonversion3和更高版本。我也不想用tKinter。在

如果有人可以通过添加几行代码来解决这个问题,这将是一个很大的帮助,因为这是我的计算机科学课程


Tags: 模块import版本密码windows错误字母password
1条回答
网友
1楼 · 发布于 2024-09-30 03:22:36

我想我正是你所需要的。 密码设置为变量名“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()

丹,有什么改进吗

相关问题 更多 >

    热门问题