caesar密码的Tkinter GUI无法按预期工作

时间:2017-11-13 01:50:00

标签: python tkinter caesar-cipher

我为凯撒密码程序制作了一个tkinter GUI,但它无法正常工作。 我在第一个输入框中键入我的消息,在第二个输入框中输入密钥,然后单击加密/解密,结果显示在第三个输入框中。但我从来没有得到正确的结果。

有时如果我使用高于6的密钥,或输入多个单词,我会收到以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:/Users/PC/PycharmProjects/oyun/sezarUIing.py", line 33, in Encrypt
self.translation = self.translation + self.LETTERS[sayı]
IndexError: string index out of range

这是代码:

from tkinter import *

class Sezar(Frame):
    def __init__(self,pencere):
        Frame.__init__(self,pencere)
        self.pencere = pencere

        self.Lab1 = Label(pencere, text="Enter your message: ",relief= GROOVE, width=20).place(x=20,y=30)

        self.Lab2 = Label(pencere, text="Enter key: ", relief=GROOVE, width=20).place(x=20, y=90)

        self.Ent1 = Entry(pencere,width=30)
        self.Ent1.place(x=170,y=30)

        self.Ent2 = Entry(pencere,width=30)
        self.Ent2.place(x=170,y=90)

        self.But1 = Button(pencere, text="Encrypt", relief=GROOVE,font="bold",command= self.Encrypt).place(x=50,y=150)
        self.But1 = Button(pencere, text="Decrypt", relief=GROOVE, font="bold",command= self.Decrypt).place(x=110, y=150)

        self.RESULT = Entry(pencere, width=30)
        self.RESULT.place(x=170,y=200)

        self.LETTERS = "abcdefghijklmnopqrstuvwxyz"
        self.translation = ""


    def Encrypt(self):
        for num in self.Ent1.get():
            if num in self.LETTERS:
                sayı = self.LETTERS.find(num)
                sayı = sayı + int(self.Ent2.get())
                self.translation = self.translation + self.LETTERS[sayı]
                self.RESULT.insert(0,self.translation)
            else:
                self.translation = self.translation + num


    def Decrypt(self):
        for num in self.Ent1.get():
            if num in self.LETTERS:
                sayı = self.LETTERS.find(num)
                sayı = sayı - int(self.Ent2.get())
                if sayı >= 0:
                    sayı = sayı - len(self.LETTERS)
                elif sayı <= 0:
                    sayı = sayı + len(self.LETTERS)
                self.translation = self.translation + self.LETTERS[sayı]
                self.RESULT.insert(0,self.translation)
            else:
                self.translation = self.translation + num

if __name__ == "__main__":
    root = Tk()
    root.title("Sezar")
    root.geometry("400x300+50+50")
    Sezar(root).pack(side="top",fill = "both")
    root.mainloop()

他是错误和预期的一个例子

错误示例: Error

预期结果:Expected

1 个答案:

答案 0 :(得分:0)

您的错误消息是由于您未在Encrypt()方法中使用模块化算法(您已在Decrypt()方法中实现了这种算法。)

其他问题包括在添加新文字之前不清除self.RESULT;在附加新翻译的结果之前不清除self.translation;不控制信件;不在代码中的适当位置更新self.RESULT(如果文字以数字结尾,则不会在翻译中反映出来。)

以下是我对代码的修改,解决了上述问题:

from tkinter import *

class Sezar(Frame):
    LETTERS = "abcdefghijklmnopqrstuvwxyz"

    def __init__(self, pencere):
        Frame.__init__(self, pencere)
        self.pencere = pencere

        Label(pencere, text="Enter your message: ", relief=GROOVE, width=20).place(x=20, y=30)
        self.Ent1 = Entry(pencere, width=30)
        self.Ent1.place(x=170, y=30)

        Label(pencere, text="Enter key: ", relief=GROOVE, width=20).place(x=20, y=90)
        self.Ent2 = Entry(pencere, width=30)
        self.Ent2.place(x=170, y=90)

        Button(pencere, text="Encrypt", relief=GROOVE, font="bold", command=self.Encrypt).place(x=50, y=150)
        Button(pencere, text="Decrypt", relief=GROOVE, font="bold", command=self.Decrypt).place(x=110, y=150)

        self.RESULT = Entry(pencere, width=30)
        self.RESULT.place(x=170, y=200)

    def Encrypt(self):
        key = int(self.Ent2.get())
        length = len(self.LETTERS)

        translation = ''

        for character in self.Ent1.get():
            if character.lower() in self.LETTERS:
                sayı = self.LETTERS.find(character.lower())
                sayı = (sayı + key) % length
                translation += self.LETTERS[sayı]
            else:
                translation += character

        self.RESULT.delete(0, END)
        self.RESULT.insert(0, translation)

    def Decrypt(self):
        key = int(self.Ent2.get())
        length = len(self.LETTERS)

        translation = ''

        for character in self.Ent1.get():
            if character.lower() in self.LETTERS:
                sayı = self.LETTERS.find(character.lower())
                sayı = (sayı - key) % length
                translation += self.LETTERS[sayı]
            else:
                translation += character

        self.RESULT.delete(0, END)
        self.RESULT.insert(0, translation)

if __name__ == "__main__":
    root = Tk()
    root.title("Sezar")
    root.geometry("400x300+50+50")
    Sezar(root).pack(side="top", fill="both")
    root.mainloop()