【发布时间】:2021-08-12 18:42:11
【问题描述】:
很久以前,我看过一个关于如何使用密钥/密码加密文件(任何类型)的教程 原始代码只是在终端中制作流程,但我想将其制作成使用 tkinter 作为我的 GUI 的应用程序,我遇到了一个我的小脑袋无法解决的问题
原视频:https://www.youtube.com/watch?v=HHlInKhVz3s
这是我得到的错误:TypeError: Encrypt() missing 2 required positional arguments: 'WhichFile' and 'KeyInput'
这是我的代码:
from tkinter.filedialog import askopenfilename
import time
root = Tk()
root.title=("Tkinter Calculator")
root.geometry("500x500")
#title
WindowTitle = Label(root, text="Choose Action", font=("Arial", 15))
WindowTitle.place(x=250, y=10,anchor="center")
### The functions
#Encrypt
def Encrypt(WhichFile, KeyInput):
file = open(WhichFile, "rb")
data = file.read()
file.close()
data = bytearray(data)
for index, value in enumerate(data):
data[index] = value ^ KeyInput
file = open("CC-" + WhichFile, "wb")
file.write(data)
file.close()
#Decrypt
def Decrypt(WhichFile, KeyInput):
file = open(WhichFile, "rb")
data = file.read()
file.close()
data = bytearray(data)
for index, value in enumerate(data):
data[index] = value ^ KeyInput
file = open(WhichFile, "wb")
file.write(data)
file.close()
#Step1 - Write the name of the file(Needs to be in the same folder(Also include ext.))
WhichFile = Entry(root, width = 20)
WhichFile.place(x=100, y=150)
WhichFile.insert(0, "Enter File name with extension")
#Step2 - Ask for a key/password
KeyInput = Entry(root, width = 20)
KeyInput.place(x=100, y=250)
KeyInput.insert(0, "Enter a key: ")
#Button for encrypt
Encryptbtn = Button(root, text="Encrypt", highlightbackground='#3E4149', command=Encrypt)
Encryptbtn.place(x=100, y=350)
#Button for decrypt
Decryptbtn = Button(root, text="Decrypt", highlightbackground='#3E4149', command=Decrypt)
Decryptbtn.place(x=200, y=350)
root.mainloop()
【问题讨论】:
-
你有
command=Encrypt。这意味着tkinter将在没有参数的情况下调用Encrypt(),但您的Encrypt函数需要两个参数WhichFile和KeyInput。
标签: python tkinter encryption