【问题标题】:So i am making this file encrypter and decrypter, and now i am trying to make it an application using Tkinter GUI. got this error所以我正在制作这个文件加密器和解密器,现在我正在尝试使用 Tkinter GUI 使它成为一个应用程序。得到这个错误
【发布时间】: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 函数需要两个参数WhichFileKeyInput

标签: python tkinter encryption


【解决方案1】:

所以错误出现在这一行:

Encryptbtn = Button(root, text="Encrypt", highlightbackground='#3E4149', command=Encrypt)

将带有参数的函数传递给 Button 的“命令”

您必须将参数传递给需要参数“WhichFile”和“KeyInput”的函数 Encrypt()。 您可以使用 lambda 关键字在 Button 声明中传递参数:

Button(root, text="Encrypt", highlightbackground='#3E4149', command=lambda:Encrypt(file,input))

当您单击按钮时,它将采用“文件”和“输入”的值,记住它,因为有时它不是您真正想要的(例如Python Tkinter button callback)。 如果您希望参数在创建按钮时被“记住”,请使用柯里化 (What is 'Currying'?, More about "lambda")。


因此,首先您必须将参数传递给该函数。正如我所看到的,你有点不明白它是如何工作的,因为你试图在函数声明中使用参数,就像它们是某个东西的全局变量一样(def Encrypt(WhichFile, KeyInput) 然后作为“赋值”@987654327 @) 但它不是那样工作的,传递给函数的参数是在函数调用中指定的,例如foo(argument1,argument2),目前还没有定义。

从条目中获取文本:

您必须知道,WhichFile = Entry(root, width = 20) 不是将 Entry 的值分配给 WhichFile 变量,而是将 Entry 本身(使用 print(WhichFile) 进行检查)。我建议更改该变量的名称,例如“user_input_file”或某事。 如果要获取在条目中键入的文本,请使用user_input_file.get()。 与 KeyInput Entry 完全相同。

接下来,您必须创建一个指向该值的变量(python 变量是指针),并以我之前提到的方式在该函数中分配它(使用 lambda:)。 写成全局变量就行了,例如:

WhichFile = user_input_file.get()
KeyInput = user_input_key.get()

当然是在声明 user_input_file 和 user_input_key 之后。

我认为这会解决你的问题,请随时询问更多信息。

【讨论】:

  • fileinput 定义在哪里?使用lambda时不能传入未定义的变量@
  • 这是一个参数的例子
  • 哦,好吧,问题似乎比我想象的要大,等一下我会编辑这个
猜你喜欢
  • 2021-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 2021-12-25
  • 2020-11-10
相关资源
最近更新 更多