我有一個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 '用戶名已經存在。請選擇另一個用戶名!'
password = str(input('輸入密碼(最少8個字符,不應包含空格或超過16個字符):')
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'/span>, password.encode('utf-8'/span>), salt, 100000)
passhash = binascii.hexlify(passhash)
final_password = (salt passhash).Decode('ascii')
if c_password != password:
return 'Password do not match!'。
else:
#user_file0 = str(uuid.uuid4().hex) '.txt'(用于隨機檔案名)
user_file0 = name '_file.txt'。
fw = open(signup_file,'a'/span>)
entry = name ' ' final_password ' ' user_file0 ' '
'/span>
fw.write(entry)
return '注冊完成'。
fw.close()
f.close()
我有許多其他的函式和一個基于空閑的選單,那么有沒有辦法在不重寫整個代碼的情況下將其轉換為一個gui呢?
uj5u.com熱心網友回復:
第一步,修改你的注冊函式,將輸入作為引數,而不是使用input。
def signup(signup_file, name, password)。
f = open(signup_file,'r'/span>)
if len(name)< 4 or ' ' in name:
return ' Invalid username input'
if user_exists(name) == True:
return '用戶名已經存在。請選擇另一個用戶名!'/span>
if password == name:
return '請不要使用用戶名作為密碼! '
if ' ' in password or len( password) > 16 or len(password)<8:
return 'Invaild password input'.
#Salted-hashing[/span]。
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
passhash = hashlib.pbkdf2_hmac('sha512'/span>, password.encode('utf-8'/span>), salt, 100000)
passhash = binascii.hexlify(passhash)
final_password = (salt passhash).Decode('ascii')
#user_file0 = str(uuid.uuid4().hex) '.txt'(用于隨機檔案名)
user_file0 = name '_file.txt'。
fw = open(signup_file,'a'/span>)
entry = name ' ' final_password ' ' user_file0 ' '
'
fw.write(entry)
fw.close()
f.close()
return '注冊完畢'
順便說一下,你應該只在關閉檔案后回傳!
密碼匹配檢查被移除,這應該在GUI中完成。
第二步,制作你的 GUI 并讓它呼叫該函式。這里有一個例子,使用ascii-designer庫(宣告:我是作者)...
from ascii_designer import set_toolkit, AutoFrame
set_toolkit('tk')
class SignupForm(AutoFrame)。
f_body = ""
| |
結果
名稱。 [ 名字_ ]
密碼: [ password_ ]
確認。 [ cpassword_ ]
[ 確定 ]
""
def f_on_build(self)。
self.label_result = ''。
self.name = '' '' ''
# TODO: 設定密碼和cpassword,不顯示明文。
def on_ok(self)。
if self.password != self.cpassword:
self.label_result = '密碼不匹配'。
else:
result = signup('accounts.txt', self.name, self.password)
self.label_result = result
if __name__ == '__main__'/span>:
SignupForm().f_show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/329560.html
標籤:
上一篇:如何減少頁面中的串列空間?
