首页 > 解决方案 > 如何从输入字段中获取数据并在 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

标签: pythontkinter

解决方案


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()

查看代码的变化


推荐阅读