话不多说,直接上代码
import tkinter as tk
import time
import tkinter.filedialog as tf
line = 0
def pprint(msg, text: tk.Text):
global line
nowtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
text.insert(tk.END, f" {line} {nowtime} {msg}\n")
line += 1
def jmonefile(files, num, text: tk.Text):
try:
nums = list(str(num))
except ValueError:
pprint(f"in file {files},input error:bad value(only number)(加密)", text)
return
try:
with open(files, encoding="utf-8") as f:
aa = f.read()
except OSError as oe:
pprint(f"in file {files},got an ERROR:文件出现错误({oe})(加密)", text)
except UnicodeError as ue:
pprint(f"in file {files},got an ERROR:文件出现编码问题({ue})(我们只支持编码为utf-8的文件)(加密)", text)
else:
with open(files, 'w', encoding='utf-8') as f:
i = 0
for a in list(aa):
f.write(chr(ord(a) + int(nums[i])))
pprint(f"in file {files},DONE:完成!(加密)", text)
def unjmonefile(files, num, text: tk.Text):
try:
nums = list(str(num))
except ValueError:
pprint(f"in file {files},input error:bad value(only number)(解密)", text)
return
try:
with open(files, encoding="utf-8") as f:
aa = f.read()
except OSError as oe:
pprint(f"in file {files},got an ERROR:文件出现错误({oe})(解密)", text)
except UnicodeError as ue:
pprint(f"in file {files},got an ERROR:文件出现编码问题({ue})(解密)", text)
else:
with open(files, 'w', encoding="utf-8") as f:
i = 0
for a in list(aa):
f.write(chr(ord(a) - int(nums[i])))
pprint(f"in file {files},DONE:完成!(解密)", text)
def f1blowse():
a = tf.askopenfilename()
if a == '':
return
f1.set(a)
def f2blowse():
a = tf.askopenfilename()
if a == '':
return
f2.set(a)
def dojm(entry: tk.Entry, text: tk.Text):
a = f1.get()
jmonefile(files=a, num=entry.get(), text=text)
def dounjm(entry: tk.Entry, text: tk.Text):
a = f2.get()
unjmonefile(files=a, num=entry.get(), text=text)
root = tk.Tk()
f1 = tk.StringVar()
f2 = tk.StringVar()
root.title("加密小工具:by 曹高远,version=0.0.1")
root.geometry("700x500")
root.minsize(700, 500) # 最小尺寸
root.maxsize(700, 500) # 最大尺寸
fr1 = tk.Frame(root, bg='yellow')
fr1.grid(row=0, column=0, )
fr2 = tk.Frame(root)
fr2.grid(row=1, column=0)
fr3 = tk.Frame(root, bg='blue')
fr3.grid(row=2, column=0)
text = tk.Text(fr3)
text.pack()
lb1 = tk.Label(fr1,
text=u'TIPS:\n文本加密小工具--version=0.0.1,目前只支持编码为“UTF-8”的文件。\n每一个用此工具加密过的文件都有一个密码(其中不益\
有0,因为如果有0,文件的安全性会遭到破坏)。\n再你加密的时候,你需要创建并记住它(即密码),否则再你解密之时你就惨了。。。\n如果你把两个文件加\
密了2次(及以上),那么你也要解密2次(及以上)。一般来说,加密的次数越多,文件越复杂。\n不过,加密最好不超过10次,要不然本程序会中途崩溃。。。',
bg='yellow', width=100)
lb1.grid(row=0, column=0)
en1 = tk.Entry(fr2, textvariable=f1, width=30)
en2 = tk.Entry(fr2, width=15)
en1.grid(row=0, column=0, padx=25, pady=30)
en2.grid(row=0, column=2, padx=25, pady=30)
btn1 = tk.Button(fr2, text=u"浏览(B)...", command=f1blowse)
btn1.grid(row=0, column=1)
btn2 = tk.Button(fr2, text=u"加密!", command=lambda: dojm(en2, text))
btn2.grid(row=0, column=3)
en3 = tk.Entry(fr2, textvariable=f2, width=30)
en4 = tk.Entry(fr2, width=15)
en3.grid(row=1, column=0, padx=25, pady=30)
en4.grid(row=1, column=2, padx=25, pady=30)
btn3 = tk.Button(fr2, text=u"浏览(B)...", command=f2blowse)
btn3.grid(row=1, column=1)
btn4 = tk.Button(fr2, text=u"解密!", command=lambda: dounjm(en4, text))
btn4.grid(row=1, column=3)
tk.mainloop()
虽然?GUI丑的不得了,但的确能使。。。