首先是一个小小的免责声明: 我对一般的编程很陌生,我有很多东西需要学习。所以我的代码可能看起来很草率和荒谬。所以我很抱歉。
我正在尝试创建一个简单的gui程序,它接受用户输入的文本并将其从我制作的字典中变成乱码。因此,您可以加密和解密文本。这是我制作的控制台脚本:
`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!"
`
所以现在我尝试使用tkinter实现相同的想法但使用gui。我被困在哪里试图基本上打印结果在某处。这就是我到目前为止所做的:
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()
这是我得到的错误:
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 ================
任何帮助都将非常感激。我希望我提供足够的信息来帮助您理解。谢谢大家的时间,祝大家愉快!
更新: 所以现在这是我得到的错误:
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 ================
我不明白非序列的迭代究竟意味着什么。我无法绕过谷歌不得不说的话。我以前的项目中从来没有遇到过这个错误。
更新的代码:
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()
答案 0 :(得分:2)
您传递给函数的参数类型为StringVar
,而不是string
,它是python string
类型的包装。
来自docs -
如果您使用Tcl语言编程Tk,您可以要求系统 让你知道变量的变化。 Tk工具包可以使用它 功能,称为跟踪,以在关联时更新某些小部件 变量被修改。
没有办法跟踪Python变量的变化,但是Tkinter 允许您创建可在Tk的任何地方使用的变量包装器 可以使用跟踪的Tcl变量。
您只需更改此行
即可results = encrypt(encrypt_entry_var)
到
results = encrypt(encrypt_entry_var.get())
固定代码 -
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()