python - 如何从输入字段中获取数据并在 Tkinter 的函数中使用它
问题描述
我使用这段代码在 Tkinter 中创建了一个加密器。
普通 python 上的程序后端按预期运行。当我使用 Tkinter 并使用提交按钮为密码和要加密的数据创建表单时,我使用 Tkinter 的 Entry 功能来输入密码和要输入的数据。但是当我使用该.get()
函数将数据存储在变量中并执行加密任务时,我不能使用该.get()
方法作为.grid
或.pack()
两者都创建入口函数NoneType
,并且当我在使用.get()
之前使用该函数时.grid()
,.pack()
错误是 str type 没有网格或打包功能。
我是初学者,我不知道这是什么问题,请帮助我。
该代码是:
from tkinter import *
root = Tk()
root.title("Encryptor")
def encrypt(sentence, key):
asc_sentence = []
asc_key = []
encryp = []
encrypted = ""
for letter in key:
le = ord(letter) - 17
asc_key.append(le)
asc_sum = sum(asc_key)
for letter in sentence:
lex = ord(letter) + asc_sum
asc_sentence.append(lex)
for i in asc_sentence:
lconv = chr(i)
encryp.append(lconv)
for index in encryp:
encrypted += index
enc = Label(root, textvariable=encrypted)
enc.pack()
def input_enc():
intro = Label(root, text="------------------------- Encrypt your Data----------------------")
intro.pack()
pwd_l = Label(root, text="Enter your Key to encrypt the data: ")
pwd_l.pack()
password = Entry(root, width=25).pack()
data_l = Label(root, text="Input the data You want to Encrypt: ")
data_l.pack()
data = Entry(root, width=25).pack()
edata = data.get()
epwd = password.get()
submit = Button(root, text="Encrypt", command=lambda: encrypt(edata, epwd))
submit.pack()
input_enc()
root.mainloop()
错误是:
Traceback (most recent call last):
File "/home/lord_hendrix17/PycharmProjects/P0/main.py", line 46, in <module>
input_enc()
File "/home/lord_hendrix17/PycharmProjects/P0/main.py", line 41, in input_enc
edata = data.get()
AttributeError: 'NoneType' object has no attribute 'get'
Process finished with exit code 1
解决方案
from tkinter import *
root = Tk()
root.title("Encryptor")
def encrypt(sentence, key):
#print(sentence)
#print(key)
asc_sentence = []
asc_key = []
encryp = []
encrypted = ""
for letter in key:
le = ord(letter) - 17
asc_key.append(le)
asc_sum = sum(asc_key)
for letter in sentence:
lex = ord(letter) + asc_sum
asc_sentence.append(lex)
for i in asc_sentence:
lconv = chr(i)
encryp.append(lconv)
for index in encryp:
encrypted += index
enc = Label(root, textvariable=encrypted)
enc.pack()
def input_enc():
edata=StringVar()#!!!
epwd=StringVar()#!!!
intro = Label(root, text="------------------------- Encrypt your Data----------------------")
intro.pack()
pwd_l = Label(root, text="Enter your Key to encrypt the data: ")
pwd_l.pack()
password = Entry(root, textvariable=epwd,width=25).pack()#!!!
data_l = Label(root, text="Input the data You want to Encrypt: ")
data_l.pack()
data = Entry(root, textvariable=edata,width=25).pack()#!!!
#edata = data.get()
#epwd = password.get()
submit = Button(root, text="Encrypt", command=lambda: encrypt(edata.get(), epwd.get()))#!!!
submit.pack()
input_enc()
root.mainloop()
查看代码的变化
推荐阅读
- wpf - 尝试在 xaml 中使用系统命名空间时出错
- reactjs - 使用复活的自定义开关组件
- c - 了解 getpid() 系统调用的 strace
- flutter - .cast() 和 List.from() 和 List.castFrom() 有什么区别
- laravel - 仅当存在 post 值时,如何在 Laravel 中使用闭包检查条件
- java - Kotlin 中的 XOR 运算符是功能还是错误?
- python - 在 PythonAnywhere 上升级 Python 3.8 后 Dash WSGI 文件出现问题
- java - 如何在 JAVA 中处理 ArrayList?
- angular - 错误类型错误:无法读取对象中不存在的属性“名称”
- amazon-web-services - AWS CodeDeploy“不允许跨账户传递角色”错误