【问题标题】:Can I use a python program and convert it into a gui using tkinter?我可以使用 python 程序并使用 tkinter 将其转换为 gui 吗?
【发布时间】:2021-09-20 06:26:10
【问题描述】:

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

程序中的一个 sn-p:

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?

【问题讨论】:

  • 对不起,你得从头开始。
  • 这是一个常见的问题:如何通过各种人机界面使用相同的处理。答案是低耦合。如果您将算法部分封装在函数和类中,您将能够共享它。但如果更复杂的部分是接口本身,唯一的好处就是可维护性:如果您以后使用不同的密码编码方案,您将在一个地方更改它。
  • @SergeBallesta 抱歉,但这对我的学校项目来说似乎太复杂了????

标签: python user-interface tkinter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 2016-08-25
    相关资源
    最近更新 更多