Home > front end >  Can I use a python program and convert it into a gui using tkinter?
Can I use a python program and convert it into a gui using tkinter?

Time:09-22

I have a python password management system program and I am looking to see if I can convert it into a gui based program.

a snippet from the program :

def signup():
    f = open(signup_file,'r')
    print('Sign-up procedure')
    print()
    name = str(input('Enter your username (minimum 4 characters, no whitespaces) : '))
    if len(name)<4 or ' ' in name:
        return 'Invalid username input'
    if user_exists(name) == True:
        return 'Username already exists. Please choose another username!'
    password = str(input('Enter your password (minimum 8 characters, should not contain whitespace or exceed 16 characters): '))
    if password == name:
        return 'Please dont use the username as the password! '
    if ' ' in password or len(password)>16 or len(password)<8:
        return 'Invaild password input'
    
    c_password=str(input('Confirm your entered password : '))
    
    #Salted-hashing
    salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
    passhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), salt, 100000)
    passhash = binascii.hexlify(passhash)
    final_password = (salt   passhash).decode('ascii')
    
    if c_password != password:
        return 'Passwords do not match !'
    else:
        #user_file0 = str(uuid.uuid4().hex) '.txt' (for random file name)
        user_file0 = name   '_file.txt'
        fw = open(signup_file,'a')
        entry = name   ' '   final_password   ' '   user_file0   '\n'
        fw.write(entry)
        return 'Sign-up completed'
        fw.close()
    f.close()

I have many other functions and a menu based on idle, so is there any way to convert this into a gui without rewriting the entire code?

CodePudding user response:

First step, modify your signup function to take the inputs as parameters instead of using input.

def signup(signup_file, name, password):
    f = open(signup_file,'r')
    if len(name)<4 or ' ' in name:
        return 'Invalid username input'
    if user_exists(name) == True:
        return 'Username already exists. Please choose another username!'
    if password == name:
        return 'Please dont use the username as the password! '
    if ' ' in password or len(password)>16 or len(password)<8:
        return 'Invaild password input'
    
    #Salted-hashing
    salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
    passhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), salt, 100000)
    passhash = binascii.hexlify(passhash)
    final_password = (salt   passhash).decode('ascii')
    
    #user_file0 = str(uuid.uuid4().hex) '.txt' (for random file name)
    user_file0 = name   '_file.txt'
    fw = open(signup_file,'a')
    entry = name   ' '   final_password   ' '   user_file0   '\n'
    fw.write(entry)
    fw.close()
    f.close()
    return 'Sign-up completed'

By the way, you should only return after closing the files!

The passwords-match check was removed, this should be done in the GUI.

Second step, make your GUI and have it call the function. Here an example, using the ascii-designer library (Disclaimer: I am the author)...

from ascii_designer import set_toolkit, AutoFrame

set_toolkit('tk')

class SignupForm(AutoFrame):
    f_body = """
        |           |
         result
         Name:       [ name_      ]
         Password:   [ password_  ]
         Confirm:    [ cpassword_ ]
                        [ OK ]
    """
    def f_on_build(self):
        self.label_result = ''
        self.name = ''
        self.password = ''
        self.cpassword = ''
        # TODO: Setup password and cpassword to not show cleartext.

    def on_ok(self):
        if self.password != self.cpassword:
            self.label_result = 'Passwords do not match'
        else:
            result = signup('accounts.txt', self.name, self.password)
            self.label_result = result

if __name__ == '__main__':
    SignupForm().f_show()
  • Related