首页 > 解决方案 > 我可以使用 python 程序并使用 tkinter 将其转换为 gui 吗?

问题描述

我有一个 python 密码管理系统程序,我正在寻找是否可以将其转换为基于 gui 的程序。

程序片段:

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()

我有许多其他功能和一个基于空闲的菜单,那么有没有办法将其转换为 gui 而无需重写整个代码?

标签: pythonuser-interfacetkinter

解决方案


第一步,修改您的注册函数以将输入作为参数,而不是使用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'

顺便说一句,你应该只return在关闭文件之后!

密码匹配检查已被删除,这应该在 GUI 中完成。

第二步,制作你的 GUI 并让它调用函数。这是一个使用ascii-designer库的示例(免责声明:我是作者)...

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()

推荐阅读