NameError:未定义全局名称“文本" [英] NameError: global name 'text' is not defined

查看:31
本文介绍了NameError:未定义全局名称“文本"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 代码片段,它使用凯撒密文将 Mac 地址转换为另一个代码:代码如下:

I have a python code snippet that coverts the Mac address to another code using caesar ciphertext: The code is given below:

import uuid
def getmac():
  mac_num = hex(uuid.getnode()).replace('0x', '').upper()
  mac = ''.join(mac_num[i : i + 2] for i in range(0, 11, 2))
  return mac

plaintext = getmac()
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
key = 1
cipher = ''

for c in plaintext:
    if c in alphabet:
      cipher += alphabet[(alphabet.index(c) + key +3)%(len(alphabet))]
print('Code:' + cipher)

这可以正常工作,因为它将代码打印到 Python shell,但是当我使用 TKinter 库编辑相同的代码时,出现连接和其他错误,TKinter 库中使用的代码如下:在这个片段中,程序的功能是相同的,但是我只想从用户输入 mac 地址,当他点击提交时,代码会提示给他:

This works proper as it prints out the code to the Python shell, however when i Edit the same code with TKinter library, I get concatenation and other errors , The code used in TKinter lib is given below: In this snippet the function of the program is same ,however i just want the mac address to be input from the user and when he clicks on submit ,the code is prompted to him:

import uuid
from Tkinter import *

root = Tk()

root.title("Code Generator")
root.geometry("250x200+200+100")
root.resizable(width=False, height=False)
cipher = ''


Label(root, text='Mac Address:').grid(row=0, sticky=W, padx=4)
Entry(root).grid(row=0, column=1, sticky=E, pady=4)

Label(root, text="Code:").grid(row=1, sticky=W, padx=4)
hlbl = Label(root, text=cipher,  width=20)
hlbl.grid(row=0, column=2, sticky=E, pady=4)


def get_it():
 alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  for c in text:
    if c in alphabet:
      cipher += alphabet[(alphabet.index(c) + key + 2) % (len(alphabet))]

Button(root, text="Submit", command=get_it).grid(row=2, column=1)
root.mainloop()

当我运行程序时,我得到了这个:

当我输入随机文本并单击提交时,我得到以下信息:

我应该做哪些改变?

when i run the program i get this:

When i enter a random text and click submit i get this:

What changes should i make?

推荐答案

首先你需要为 tkinter 命名 Entry 以便你以后可以引用它,然后使用 get> 获取 Entry 文本的方法.

First you need to name the tkinter Entry so you can reference to it later, then use the get method to get the Entry text.

这是修改后的代码;

import uuid
from Tkinter import *

root = Tk()

root.title("Code Generator")
root.geometry("250x200+200+100")
root.resizable(width=False, height=False)
key = 1
cipher = ''

label_text = StringVar()
#label_text.set(cipher)


Label(root, text='Mac Address:').grid(row=0, sticky=W, padx=4)
entry = Entry(root)
entry.grid(row=0, column=1, sticky=E, pady=4)

Label(root, text="Code:").grid(row=1, sticky=W, padx=4)
hlbl = Label(root, textvariable=label_text,  width=20)
hlbl.grid(row=1, column=1, sticky=E, pady=4)


def get_it():
    global key, cipher
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    text =  entry.get() # get contents of entry
    for c in text:
        if c in alphabet:
            cipher += alphabet[(alphabet.index(c) + key + 2) % (len(alphabet))]
    label_text.set(cipher)

Button(root, text="Submit", command=get_it).grid(row=2, column=1)
root.mainloop()

这篇关于NameError:未定义全局名称“文本"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆