因此,我正在尝试进行多项选择测验,所以这是我到目前为止所做的代码,但是我不知道如何在回访用户或新用户工作时验证密码正如它应该。如果用户已经使用过该程序,则有2个功能;如果用户是新用户,则需要2个功能;需要使用用户名和密码建立新帐户
import pygame
import sys
teal= (0,150,150)
#display window
pygame.init()
size=[800,600]
screen= pygame.display.set_mode(size)
screen.fill(teal)
font=pygame.font.SysFont("comicsandms", 72)
pygame.display.update()
#textbox code
def textbox():
input_box = pygame.Rect(300, 300, 500, 50)
color_inactive = pygame.Color('lightskyblue3')
color_active = pygame.Color('dodgerblue2')
color = color_inactive
active = False
text = ''
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
# If the user clicked on the textbox
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
# change the colour of the textbox once clicked
color = color_active if active else color_inactive
if event.type == pygame.KEYDOWN:
if active:
#prints the text from the textbox in the python shell
if event.key == pygame.K_RETURN:
print(text)
text = ''
done= True
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
# Render the current text.
text_surface = font.render(text, True, color)
# Resize the box if the text is too long.
width = max(200, text_surface.get_width()+10)
input_box.w = width
# Blit the text.
screen.blit(text_surface, (input_box.x+5, input_box.y+5))
# Blit the input_box rect.
pygame.draw.rect(screen, color, input_box, 2)
pygame.display.flip()
#username and password functions
def username():
usernametext= font.render("username :", True, (100, 100, 175))
screen.blit(usernametext, (400 - usernametext.get_width() // 2, 240 - usernametext.get_height() // 2))
textbox()
#text
def password():
passwordtext= font.render("password :", True, (100, 100, 175))
screen.blit(passwordtext, (400 - passwordtext.get_width() // 2, 240 - passwordtext.get_height() // 2))
textbox()
#new user username and password functions
def newuser():
usernametext=font.render("please create a username", True, (100,100,175))
screen.blit(usernametext, (400 - usernametext.get_width() // 2, 240 - usernametext.get_height() // 2))
textbox()
def newpassword():
passwordtext= font.render("please create a password", True, (100, 100, 175))
screen.blit(passwordtext, (400 - passwordtext.get_width() // 2, 240 - passwordtext.get_height() // 2))
textbox()
def mainscreen():
#start up
font=pygame.font.SysFont("comicsandms", 72)
titletext= font.render("welcome to a-level chem quiz !!", True,(125,0,125))
screen.blit(titletext,(400 - titletext.get_width() // 2, 240 - titletext.get_height() // 2))
logintext= font.render("have you got an account?", True ,(0,0,0))
screen.blit(logintext,(400 - logintext.get_width() // 2, 300 - logintext.get_height() // 2))
#button for having account
yesbutton= pygame.Rect(200,350,100,100)
pygame.draw.rect(screen,[0,200,50], yesbutton)
yesbuttontext=font.render("yes", True, (0,0,0))
screen.blit(yesbuttontext, (250 - yesbuttontext.get_width() // 2, 400 - yesbuttontext.get_height() //2))
#button for not having account
nobutton= pygame.Rect(450, 350, 100,100)
pygame.draw.rect(screen,[200,0,50], nobutton)
nobuttontext=font.render("no", True, (0,0,0))
screen.blit(nobuttontext, (513 - yesbuttontext.get_width() // 2, 400 - nobuttontext.get_height() //2))
pygame.display.update()
done=False
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done= True
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos= pygame.mouse.get_pos()
#if they already have an account
if yesbutton.collidepoint(mouse_pos):
screen.fill((30,30,30))
username()
screen.fill((30,30,30))
password()
done = True
#if they are a new user and need to create a username and password
elif nobutton.collidepoint(mouse_pos):
screen.fill((30,30,30))
newuser()
screen.fill((30,30,30))
newpassword()
done= True
pygame.display.update()
if __name__ == "__main__":
mainscreen()
任何帮助将不胜感激,谢谢:) 抱歉,这是一大堆代码
答案 0 :(得分:0)
@groot首先,标题不清楚。更合适的标题是“如何在Python中验证密码”。我以为您在谈论Tkinter文本字段。另外,字体是“ comicsansms”而不是“ comicsandms”。阅读您的代码和问题正文后,我了解了。您需要研究Python中的I \ O。那就是读取文件内容的方法。将用户名和密码写入文件后:脚本可以找到该文件并验证用户名和密码是否存在以及它们是否匹配。我已经很长时间没有使用它了,所以我不太记得它是如何工作的,但是如果您查找“ Python I / O”,就会发现它。
答案 1 :(得分:0)
我不确定您要如何存储用户信息,因为您当前仅将其存储在变量中,该变量仅在程序的当前实例期间起作用,并且在程序关闭后消失,因此我包括以下用于存储用户数据,添加和验证用户数据的功能。
import io
import ConfigParser
def create_User(username, password):
config = ConfigParser.RawConfigParser()
config.add_section('UserInformation')
config.set('UserInformation', username, password)
with open('userinfo.ini', 'wb') as f:
config.write(f)
print('User: ' + username + 'added to file with password: ' + password)
def check_User(username, password):
with open('userinfo.ini') as f:
conf = f.read()
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(conf))
real_Password = config.get('UserInformation', username)
if real_Password == password:
return True
else:
return False
#create_User('sage', 'password')
if check_User('sage', 'password') != False:
print ('User Correct!')
else:
print('User False!')
它使用了ini,它是一个简单的Config,可以使用,该代码正在运行,应该可以为您提供帮助。
玩得开心