NameError: Global Name 'text' Is Not Defined
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()
Solution 1:
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.
Here is the modified code;
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()
Post a Comment for "NameError: Global Name 'text' Is Not Defined"