python - 我可以使用 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 而无需重写整个代码?
解决方案
第一步,修改您的注册函数以将输入作为参数,而不是使用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()
推荐阅读
- django - Django 在实例删除时错误地触发了 post_save 信号
- grafana - 如何在 Grafana 中指定比 10ms 更精细的间隔?
- python - 分别计算求和参数
- ruby - 加载共享库时出错:libxml2.so.2:无法打开共享对象文件:将 jruby 版本更改为 9.2.6.0 时没有此类文件或目录
- mysql - 仅限 pSQL 数组字段
- c# - 与 Process Explorer 的 TID 对应的 System.Threading.Thread 的属性/属性/字段是什么?
- apl - 涉及赋值的表达式的意外值
- three.js - 如何在 Threejs 中绘制开口曲线形状
- twitter - Twitter v1 流 API 返回错误提及
- google-cloud-platform - 您将如何从外部环境访问 Google Secret Manager?