用于Caesar的Python GUI';s密码函数
我在下面有两个函数对文本文件中的文本进行编码和解码。 首先,我去掉所有非字母字符,并将文本转换为小写。 encode函数将文本中的每个字符移动一定的位置(数字到旋转)。 最后,解码功能将按指定的顺序打印每种可能的字符组合,其编号为“旋转” 我想用python创建一个GUI来接受两个输入(字符串和整数)并输出密文,而不是从文本文件中读取用于Caesar的Python GUI';s密码函数,python,user-interface,tkinter,Python,User Interface,Tkinter,我在下面有两个函数对文本文件中的文本进行编码和解码。 首先,我去掉所有非字母字符,并将文本转换为小写。 encode函数将文本中的每个字符移动一定的位置(数字到旋转)。 最后,解码功能将按指定的顺序打印每种可能的字符组合,其编号为“旋转” 我想用python创建一个GUI来接受两个输入(字符串和整数)并输出密文,而不是从文本文件中读取 import collections import string import re with open('random.txt','r') as f:
import collections
import string
import re
with open('random.txt','r') as f:
data = f.read().replace('\n', '')
data_formatted = re.sub('[^A-Za-z]+', '', data.lower())
def encode(rotate_string, number_to_rotate_by):
lower = collections.deque(string.ascii_lowercase)
lower.rotate(number_to_rotate_by)
lower = ''.join(list(lower))
return rotate_string.translate(str.maketrans(string.ascii_lowercase, lower))
encode(data_formatted,1)
def decode(test_string):
for i in range(len(string.ascii_lowercase)):
print(encode(data_formatted, i))
decode(data_formatted)
下面是我目前对tkinter的看法。但是,我得到了以下错误
回溯(最近一次呼叫最后一次):
文件“5.py”,第28行,在
按钮=按钮(text='Press to print',command=encode(b,0),fg='red').pack()
文件“5.py”,第19行,编码
返回rotate_string.translate(str.maketrans(string.ascii_小写,小写))
AttributeError:“StringVar”对象没有属性“translate”
我将感谢任何帮助
from tkinter import *
import collections
import string
a=Tk()
a.title('Homework 1')
a.geometry('500x500+300+100')
b=StringVar()
def encode(rotate_string, number_to_rotate_by):
c=b.get()
lower = collections.deque(string.ascii_lowercase)
lower.rotate(number_to_rotate_by)
lower = ''.join(list(lower))
return rotate_string.translate(str.maketrans(string.ascii_lowercase, lower))
labl=Label(text='Please write a word or sentence',font=30).pack()
text=Entry(textvariable=b).pack()
button=Button(text='Press to print', command=encode(b,0),fg='red').pack()
a.mainloop()
有很多错误 主要问题:
StringVar
需要.get()
才能获取具有.translate()
您可以在上面的代码和注释中看到其他内容
--
请共享整个错误消息。你从中了解到了什么?顺便说一句,不鼓励星号/星号导入。在您的帖子中,注释中的代码很难找到和阅读。
Traceback(最近一次调用):文件“5.py”,第28行,在button=button(text='Press to print',command=encode(b,0),fg='red').pack()中文件“5.py”,第19行,在encode return rotate_string.translate(str.maketrans(string.ascii_lowercase,lower))AttributeError:'StringVar'对象没有应该放在文章中的属性“translate”
,too.AttributeError:“StringVar”对象没有属性“translate”:类型为tkinter的对象。StringVar
不是Pythonstr
。阅读
b.get().translate(...)
#import from tkinter import * # PEP8: `import *` not preferred
import tkinter as tk
import collections
import string
# --- functions ---
def encode(number_to_rotate_by):
lower = collections.deque(string.ascii_lowercase)
lower.rotate(number_to_rotate_by)
lower = ''.join(list(lower))
text = entry_text.get()
result = text.translate(str.maketrans(string.ascii_lowercase, lower))
#result_label["text"] = result
result_text.set(result)
# --- main ---
root = tk.Tk()
entry_text = tk.StringVar()
result_text = tk.StringVar()
label = tk.Label(root, text='Please write a word or sentence')
label.pack()
text = tk.Entry(root, textvariable=entry_text)
text.pack()
result_label = tk.Label(root, textvariable=result_text) # label for result
result_label.pack()
button = tk.Button(root, text='Press to print', command=lambda:encode(1))
button.pack()
root.mainloop()