0

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)

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:
enter image description here
When i enter a random text and click submit i get this:
enter image description here
What changes should i make?

nbro
  • 15,395
  • 32
  • 113
  • 196
  • It would help if you provided the errors you receive, the code you have provided gives `MacAdd` is undefined exception when pressing Submit. The `Entry` widget has no reference so you can't even get the input from it. – Steven Summers Mar 10 '17 at 05:23
  • @StevenSummers please check now –  Mar 10 '17 at 05:32
  • Okay, same as I explained before only `text` is now undefined. – Steven Summers Mar 10 '17 at 05:36
  • so how do i take the text that user inputs in the Entry, convert and then print it besides the code label –  Mar 10 '17 at 05:40
  • you should get the `text` user gives, see this http://stackoverflow.com/questions/14824163/how-to-get-the-input-from-the-tkinter-text-box-widget – Bijoy Mar 10 '17 at 05:41
  • @Bijoy i'm kind of new to this, can you elaborate –  Mar 10 '17 at 05:49
  • @JustinJoy Your program should get the values the user typed in `textbox` when they click submit. for that I have posted the above link – Bijoy Mar 10 '17 at 06:09

1 Answers1

2

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()
Khristos
  • 973
  • 2
  • 11
  • 22
  • Tried the code ,but when i click on submit the ouput is displayed on the python shell instead of gui and it is displayed in iterations –  Mar 10 '17 at 06:20
  • That is because you did not put in code to display the output on the gui. Use the tkinter `textvariable` so you can change the text later, and use the `.set` method to set the new text of the label. I have edited the code, try it now and see if it works the way you expected. – Khristos Mar 10 '17 at 06:31
  • Yes it works ,Thank you, just needed to ask one thing , with the previous code I understood Why it was printing on to the shell instead of the gui , but why was it printing in iterations: for eg : if input is 6C, output comes out to be ; 9 9f –  Mar 10 '17 at 06:33
  • I think it was printing the cipher variable after every iteration over 'text'. I only get that behavior if I put a print statement right after the `cipher += ...` in the body of the `if` statement. – Khristos Mar 10 '17 at 06:43