首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我可以使用python程序并使用tkinter将其转换为gui吗?

我可以使用python程序并使用tkinter将其转换为gui吗?
EN

Stack Overflow用户
提问于 2021-09-20 06:26:10
回答 1查看 50关注 0票数 0

我有一个python密码管理系统程序,我想看看我是否可以把它转换成一个基于gui的程序。

程序中的一段代码:

代码语言:javascript
运行
复制
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()

我还有很多其他的功能和基于idle的菜单,那么有没有办法在不重写整个代码的情况下把它转换成gui呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-20 06:47:52

第一步,修改您的注册函数,将输入作为参数,而不是使用input

代码语言:javascript
运行
复制
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库的例子(免责声明:我是作者)……

代码语言:javascript
运行
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69249921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档