我目前正在製作一個Python 3的程序,其中我需要用戶輸入程序稍後會使用的密碼。我現在面臨的問題是,如果我只使用password = input("Enter password: ")
,用戶輸入的字符將在屏幕上可見 - 我寧願用asterixes替換它們。在Python 3上輸入一個隱藏的字符串
當然,我可以使用pygame的和做的是這樣的:
import pygame, sys
pygame.init()
def text (string, screen, color, position, size, flag=''):
font = pygame.font.Font(None, size)
text = font.render(string, 1, (color[0], color[1], color[2]))
textpos = text.get_rect(centerx=position[0], centery=position[1])
screen.blit(text, textpos)
pygame.display.flip()
screen = pygame.display.set_mode((640, 480))
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
text('Enter password:', screen, [255, 0, 0], [320, 100], 36)
pygame.display.flip()
password = ''
password_trigger = True
while password_trigger:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if chr(int(str(event.key))) in alphabet:
password += chr(int(str(event.key)))
screen.fill((0, 0, 0))
text('*'*len(password), screen, [0, 50, 250], [320, 360], 36)
text('Enter password:', screen, [255, 0, 0], [320, 100], 36)
pygame.display.flip()
elif (event.key == pygame.K_RETURN) and (len(password) > 0):
password_trigger = False
但似乎有點矯枉過正(也Pygame的顯示器將在一個新的窗口,我的東西,我寧願避免打開) 。有沒有一個簡單的方法來做到這一點?
因爲我在家裏的房間工作,沒有陌生人,我真的很討厭密碼屏蔽。它使我無盡的悲傷。我不是唯一一個覺得這樣的人。即使在工作場所,這也不是真正的安全問題,部分是安全劇場的一種形式。我建議你讓它可選。 –