1

So first a little disclaimer: I'm pretty to new to programming in general and i have alot to learn. so my code will probably seem very sloppy and ridiculous. So i am sorry for that.

I am trying to make a simple gui program that takes text entered by the user and turns it into gibberish from a dictionary i made. So with this you can encrypt and decrypt text. this is the console script i made:

 `list = {"a": "l", "b": "k", "c": "j", "d": "h", "e": "g", "f": "d", "g": "f", "h": "d", "i": "s", "j": "a", "k": "q", "l": "w", "m": "e", "n": "r", "o": "t", "p": "y", "q": "u", "r": "z", "s": "x", "t": "c", "u": "m", "v": "n", "w": "p", "x": "b", "y": "i", "z": "o", " ": " "} 
list_2 = {"l": "a", "k": "b", "j": "c", "h": "d", "g": "e", "d": "f", "f": "g", "d": "h", "s": "i", "a": "j", "q": "k", "w": "l", "e": "m", "r": "n", "t": "o", "y": "p", "u": "q", "z": "r", "x": "s", "c": "t", "m": "u", "n": "v", "p": "w", "b": "x", "i": "y", "o": "z", " ": " "}


def encrypt():
    user_input = raw_input("what is the messge?:\n")

    for char in user_input:
        print list[char.lower()],


def decrypt():
    user_input = raw_input("what is the messge?:\n")

    for char in user_input:
        print list_2[char.lower()],


which_one = raw_input("type 1 for encrypt, type 2 for decrypt:\n")


if which_one == "1":
    encrypt()
elif which_one == "2":
    decrypt()
else:
    print "ERROR!"
`

so now I'm trying to implement the same idea but with a gui using tkinter. Where I'm stuck is trying to basically print what the result is somewhere. This is what i have so far:

    from Tkinter import *

list = {"a": "l", "b": "k", "c": "j", "d": "h", "e": "g", "f": "d", "g": "f", "h": "d", "i": "s", "j": "a", "k": "q", "l": "w", "m": "e", "n": "r", "o": "t", "p": "y", "q": "u", "r": "z", "s": "x", "t": "c", "u": "m", "v": "n", "w": "p", "x": "b", "y": "i", "z": "o", " ": " "} 
list_2 = {"l": "a", "k": "b", "j": "c", "h": "d", "g": "e", "d": "f", "f": "g", "d": "h", "s": "i", "a": "j", "q": "k", "w": "l", "e": "m", "r": "n", "t": "o", "y": "p", "u": "q", "z": "r", "x": "s", "c": "t", "m": "u", "n": "v", "p": "w", "b": "x", "i": "y", "o": "z", " ": " "}

root = Tk()
encrypt_entry_var = StringVar()
decrypt_entry_var = StringVar()


def encrypt(encrypt_entry_var):
    for char in encrypt_entry_var:
        return list

results = encrypt(encrypt_entry_var)
results_label = Label(root, str(results))



encrypt_entry = Entry(root, textvariable=encrypt_entry_var)
encrypt_button = Button(root, text="Encrypt", command=encrypt)
decrypt_entry = Entry(root, textvariable=decrypt_entry_var)
decrypt_button = Button(root, text="Decrypt")

encrypt_entry.pack(fill=X)
encrypt_button.pack(fill=X)
decrypt_entry.pack(fill=X)
decrypt_button.pack(fill=X)

root.title("Encryption Chat")
root.geometry("500x90")




mainloop()

This is the error i get:

Current directory: C:\Users\Cody Pickett\Desktop\Coding\current projects
C:\Python27\python.exe -u "C:\Users\Cody Pickett\Desktop\Coding\current projects\Encrypyion_chat_wgui.py
Process started >>>
Traceback (most recent call last):
  File "C:\Users\Cody Pickett\Desktop\Coding\current projects\Encrypyion_chat_wgui.py", line 15, in <module>
    results = encrypt(encrypt_entry_var)
  File "C:\Users\Cody Pickett\Desktop\Coding\current projects\Encrypyion_chat_wgui.py", line 12, in encrypt
    for char in encrypt_entry_var:
TypeError: iteration over non-sequence
<<< Process finished. (Exit code 1)
================ READY ================

Any help would be very appreciated.I hope i gave enough information to help you understand. Thank you all for your time and have a nice day!!!

Update: so now this is the error im getting:

Process started >>>
Traceback (most recent call last):
  File "C:\Users\Cody Pickett\Desktop\Coding\current projects\Encrypyion_chat_wgui.py", line 21, in <module>
    encrypt_button = Button(root, text="Encrypt", command=encrypt(encrypt_entry_var))
  File "C:\Users\Cody Pickett\Desktop\Coding\current projects\Encrypyion_chat_wgui.py", line 12, in encrypt
    for char in encrypt_entry_var:
TypeError: iteration over non-sequence
<<< Process finished. (Exit code 1)
================ READY ================

I do not understand what iteration over non-sequence exactly means. I could not wrap my head around what google had to say about it. I've never had this error in previous projects.

Updated code:

from Tkinter import *

list = {"a": "l", "b": "k", "c": "j", "d": "h", "e": "g", "f": "d", "g": "f", "h": "d", "i": "s", "j": "a", "k": "q", "l": "w", "m": "e", "n": "r", "o": "t", "p": "y", "q": "u", "r": "z", "s": "x", "t": "c", "u": "m", "v": "n", "w": "p", "x": "b", "y": "i", "z": "o", " ": " "} 
list_2 = {"l": "a", "k": "b", "j": "c", "h": "d", "g": "e", "d": "f", "f": "g", "d": "h", "s": "i", "a": "j", "q": "k", "w": "l", "e": "m", "r": "n", "t": "o", "y": "p", "u": "q", "z": "r", "x": "s", "c": "t", "m": "u", "n": "v", "p": "w", "b": "x", "i": "y", "o": "z", " ": " "}

root = Tk()
encrypt_entry_var = StringVar()
decrypt_entry_var = StringVar()


def encrypt(encrypt_entry_var):
    for char in encrypt_entry_var:
        return list

results = encrypt(encrypt_entry_var.get())
results_label = Label(root, text=str(results))



encrypt_entry = Entry(root, textvariable=encrypt_entry_var)
encrypt_button = Button(root, text="Encrypt", command=encrypt(encrypt_entry_var))
decrypt_entry = Entry(root, textvariable=decrypt_entry_var)
decrypt_button = Button(root, text="Decrypt")

encrypt_entry.pack(fill=X)
encrypt_button.pack(fill=X)
decrypt_entry.pack(fill=X)
decrypt_button.pack(fill=X)
results_label.pack

root.title("Encryption Chat")
root.geometry("500x90")




mainloop()
nbro
  • 15,395
  • 32
  • 113
  • 196
  • To get the text from the `StringVar()` it'll be `results = encrypt(encrypt_entry_var.get())` – Orange Mar 07 '17 at 22:35

1 Answers1

2

The argument you are passing to the function is of type StringVar which is not string, its a wrapper around the python string type.

From the docs -

If you program Tk using the Tcl language, you can ask the system to let you know when a variable is changed. The Tk toolkit can use this feature, called tracing, to update certain widgets when an associated variable is modified.

There’s no way to track changes to Python variables, but Tkinter allows you to create variable wrappers that can be used wherever Tk can use a traced Tcl variable.

What you simply need to do is change this line

results = encrypt(encrypt_entry_var)

to

results = encrypt(encrypt_entry_var.get())

FIXED CODE -

from Tkinter import *

# changed variable name
list_1 = {"a": "l", "b": "k", "c": "j", "d": "h", "e": "g", "f": "d", "g": "f", "h": "d", "i": "s", "j": "a", "k": "q", "l": "w", "m": "e", "n": "r", "o": "t", "p": "y", "q": "u", "r": "z", "s": "x", "t": "c", "u": "m", "v": "n", "w": "p", "x": "b", "y": "i", "z": "o", " ": " "} 
list_2 = {"l": "a", "k": "b", "j": "c", "h": "d", "g": "e", "d": "f", "f": "g", "d": "h", "s": "i", "a": "j", "q": "k", "w": "l", "e": "m", "r": "n", "t": "o", "y": "p", "u": "q", "z": "r", "x": "s", "c": "t", "m": "u", "n": "v", "p": "w", "b": "x", "i": "y", "o": "z", " ": " "}

root = Tk()
encrypt_entry_var = StringVar()
decrypt_entry_var = StringVar()


def encrypt(encrypt_entry_var):
    # fixed encrypt function 
    return ''.join([list_1[char.lower()] for char in encrypt_entry_var])

results = encrypt(encrypt_entry_var.get())
results_label = Label(root, text=str(results))



encrypt_entry = Entry(root, textvariable=encrypt_entry_var)
# pass a lambda function calling your encrypt function
encrypt_button = Button(root, text="Encrypt", command=lambda : encrypt(encrypt_entry_var))
decrypt_entry = Entry(root, textvariable=decrypt_entry_var)
decrypt_button = Button(root, text="Decrypt")

encrypt_entry.pack(fill=X)
encrypt_button.pack(fill=X)
decrypt_entry.pack(fill=X)
decrypt_button.pack(fill=X)
results_label.pack

root.title("Encryption Chat")
root.geometry("500x90")

mainloop()
hashcode55
  • 5,622
  • 4
  • 27
  • 40
  • thank you so much!!! now the problem i have is : File "C:\Users\Cody Pickett\Desktop\Coding\current projects\Encrypyion_chat_wgui.py", line 16, in results_label = Label(root, str(results)) File "C:\Python27\lib\lib-tk\Tkinter.py", line 2597, in __init__ Widget.__init__(self, master, 'label', cnf, kw) File "C:\Python27\lib\lib-tk\Tkinter.py", line 2091, in __init__ for k in cnf.keys(): AttributeError: 'str' object has no attribute 'keys' <<< Process finished. (Exit code 1) ================ READY ================ – thatsyntaxerrorguy Mar 07 '17 at 22:45
  • i have no idea what that error is talking about o.0 – thatsyntaxerrorguy Mar 07 '17 at 22:45
  • You are not instantiating the Label object correctly, the correct syntax is `results_label = Label(root, text=str(results))` and I do expect your `encrypt` function returns something contrary to the example stated. BTW username checks out lol – hashcode55 Mar 07 '17 at 22:50
  • oh i see its because i have str(results) ? – thatsyntaxerrorguy Mar 07 '17 at 22:50
  • @thatsyntaxerrorguy Its again because of a syntax error, you cannot pass a function which takes arguments as `command` in tkinter Button. Check [this](http://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter) out. – hashcode55 Mar 07 '17 at 23:14
  • @thatsyntaxerrorguy And also, your `encrypt` function is not going to work fine, its returning on its first iteration which it shouldn't. – hashcode55 Mar 07 '17 at 23:24
  • @thatsyntaxerrorguy Also avoid naming variables which are python keywords, like you have named `list`. – hashcode55 Mar 07 '17 at 23:26