xThekeyofdestinyx
xThekeyofdestinyx

Reputation: 1

Easygui file encoding and returning to the begin of the code

So i made an application that can login, make user, chance username and change passwords. The first problem is that i cant get file encoding to work. it saves the password in plain text (in psw.txt, but i want it to save to psw.txt encoded). The 2nd problem is, when the if loops are done, it doesnt go back to the main choice buttonbox.

Any advice would be appriciated, thank you.

from easygui import *

msg = "make a choice"
title = "test"
choices = ["login", "make user", "change username", "change password", "cancel"]  #buttonbox waarin je een aantal keuzes in kunt maken
reply = buttonbox(msg, title, choices=choices )

if reply == "make user":     # als de knop voor make user word gedrukt dan gaat deze make user multpasswordbox
  msg = "What are the User ID and Password you want to use"
  title = "Making an User"
  fieldNames = ["User ID", "Password"] #de 2 veldnamen die gebruikt worden
  fieldValues = []  # we start with blanks for the values
  fieldValues = multpasswordbox(msg,title, fieldNames)
  usr = open("usr.txt", "w")  #opent usr en psw.txt in write mode
  psw = open("psw.txt", "w", encoding='utf8')
  psw.write(str(fieldValues[1]))   #schrijft de usename in usr.txt
  usr.write(str(fieldValues[0]))  #schrijft de password in psw.txt
  psw.close()
  usr.close()
  print(fieldValues[0])     #testen om te kijken of de username en password ook opgeslagen word
  print(fieldValues[1])

  while 1:                                     #while loop die kijkt of de velden niet leeg worden gelaten
    errmsg = ""
    for i in range(len(fieldNames)):
      if fieldValues[i].strip() == "":
        errmsg = errmsg + ('"%s" is a required field.\n\n' % fieldNames[i])
    if errmsg == "": break # no problems found
    fieldValues = multpasswordbox(errmsg, title, fieldNames, fieldValues)



if reply == "login":    # als de knop voor login word gedrukt dan krijg je een multpasswordbox waarmee je kunt inloggen
  msg = "Enter logon information"
  title = "Login"
  fieldNames = ["User ID", "Password"]
  fieldValues = []  # we start with blanks for the values
  fieldValues = multpasswordbox(msg,title, fieldNames)
  psw = open("psw.txt", "r")        #opent de psw en usr.txt in leesmodus
  usr = open("usr.txt", "r")
  password = psw.read()             #leest het bestand uit en slaat het op onder "username" en "password"
  username = usr.read()
  print(fieldValues[1])             #test om te kijken of de waardes kloppen
  print(username)
  if fieldValues[0] != username and fieldValues[1] != password:             #kijkt of de username/password combinatie correct is
    msgbox("username and password is wrong")
    
  elif fieldValues[0] != username and fieldValues[1] == password:
    msgbox("Username is wrong")
    
  elif fieldValues[0] == username and fieldValues[1] != password:
    msgbox("Password is wrong")
  elif fieldValues[0] == username and fieldValues[1] == password:
    msgbox("You are logged in")
  
  psw.close()
  usr.close()
  
if reply == "change username":   #hier word de username veranderd
  msg = "enter old username and new username"
  title = "Change username"
  fieldNames = ["Old User ID", "New User ID"]
  fieldValues = []  # we start with blanks for the values
  fieldValues = multpasswordbox(msg,title, fieldNames)
  
  usr = open("usr.txt", "r")   #opent usr.txt in leesmodus

  username = usr.read() #leest het bestand uit en slaat het op onder "username"

  if fieldValues[0] != username:
    msgbox("Username is wrong")
    usr.close()
    
  elif fieldValues[0] == username:
    usr = open("usr.txt", "w")   #opent usr.txt in schrijfmodus
    usr.write(str(fieldValues[1])) #vervangt de username
    print(username)
    usr.close()

if reply == "change password":   #hier word de wachtwoord veranderd
  msg = "enter old password and new password"
  title = "Change password"
  fieldNames = ["Old password", "New password"]
  fieldValues = []  # we start with blanks for the values
  fieldValues = multpasswordbox(msg,title, fieldNames)
  psw = open("psw.txt", "r")   #opent psw.txt in leesmodus
  password = psw.read() #leest het bestand uit en slaat het op onder "username"

  if fieldValues[0] != password:
    msgbox("password is wrong")
    psw.close()
    
  elif fieldValues[0] == password:
    psw = open("psw.txt", "w",encoding='utf8')   #opent psw.txt in schrijfmodus
    psw.write(str(fieldValues[1])) #vervangt de password
    msgbox("password is changed")
    psw.close()
 

Upvotes: 0

Views: 52

Answers (0)

Related Questions