2017-03-09 143 views
0
from tkinter import * 
master = Tk() 
master.title("Caeser Cipher Program") 
master.geometry("300x200") 

frame1 = Frame(master) 
frame2 = Frame(master) 
frame3 = Frame(master) 
frame4 = Frame(master) 
frame5 = Frame(master) 
frame6 = Frame(master) 


global encryptedText 
global file 
global shiftKey 
file = "" 
encryptedText = "" 
removeSpaces = "" 
def Start(): 
    frame1.grid() 
    Question = Label(frame1, text = "Would you like to encrypt or decrypt a statement?\n\n\n") 
    Question.grid(row = 0, column = 1) 
    ButtonPlace1 = Button(frame1, text = "Encrypt", command = EncryptChosen) 
    ButtonPlace2 = Button(frame1, text = "Decrypt", command = decrypt_command) 
    ButtonPlace1.place(x = 75, y = 50, anchor = "c") 
    ButtonPlace2.place(x = 175, y = 50, anchor = "c") 

def EncryptChosen(): 
frame1.destroy() 
frame2.grid() 
Question = Label(frame2, text = "What would you like to shift it by?\t\t\t\n\n\n\n\n ") 
ButtonPlace3 = Entry(frame2) 
def SubmitEncryptionKey(): 
     shiftKey = ButtonPlace3.get() 
     frame2.destroy() 
     frame3.grid() 
     Question = Label(frame3, text = "What would you like it to say?\t\t\t\n\n\n\n\n") 
     ButtonPlace5 = Entry(frame3) 
     def SubmitText(): 
      file = ButtonPlace5.get() 
      frame3.destroy() 
      frame4.grid() 
      Question = Label(frame4, text = "Would you like to remove spaces?\t\t\t\n\n\n\n\n") 
      Question.grid(row = 0, column = 1) 
      def doRemoveSpaces(): 
        global spacesStatement 
        global removeSpaces 
        spacesStatement = "spaces will be removed" 
        removeSpaces = "True" 
        ReadyToEncrypt() 
      ButtonPlace7 = Button(frame4, text = "Yes", command = doRemoveSpaces) 
      def doNotRemoveSpaces(): 
        global spacesStatement 
        global removeSpaces 
        spacesStatement = "spaces will not be removed" 
        removeSpaces = "False" 
        ReadyToEncrypt() 
      ButtonPlace8 = Button(frame4, text = "No", command = doNotRemoveSpaces) 
      def ReadyToEncrypt(): 
        frame4.destroy() 
        frame5.grid() 
        Question = Label(frame5, text = file + "\n will be encrypted by " + shiftKey + " and \n" + spacesStatement + ".\t\t\t\n\n\n\n") 
        Question.grid(row = 0, column = 1) 
        def encryptSetup(): 

          def encrypt(character): 
           alphabet = 'abcdefghijklmnopqrstuvwxyz ' 
           character = character.lower() 
           if character.isalpha(): 
            position = str(alphabet).find(character) + shiftKey 
            if position > 25: 
             position -= 26 
            return alphabet[position] 
           else: 
            return character 

          for line in file: 
           for c in line: 
           global encryptedText 
           encryptedText = encryptedText + encrypt(c) 

          if removeSpaces == "True": 
           encryptedText = encryptedText.replace(" ","") 
          frame5.destroy() 
          frame6.grid() 
          Question = Label(frame6, text = "Here is your encrypted message: \n" + encryptedText) 
          Question.grid(row = 0, column = 1)  

        ButtonPlace9 = Button(frame5, text = "Encrypt!", command = encryptSetup) 
        ButtonPlace9.place(x=175,y=50) 
      ButtonPlace7.place(x = 75, y = 50, anchor = "c") 
      ButtonPlace8.place(x = 175, y = 50, anchor = "c") 
     ButtonPlace6 = Button(frame3, text = "Submit", command = SubmitText) 
     Question.grid(row = 0, column = 1) 
     ButtonPlace5.place(x=50,y=50) 
     ButtonPlace6.place(x=175,y=45) 
ButtonPlace4 = Button(frame2, text = "Submit", command = SubmitEncryptionKey) 
Question.grid(row = 0, column = 1) 
ButtonPlace3.place(x=50,y=50) 
ButtonPlace4.place(x=175,y=45) 



frame1.grid() 
Question = Label(frame1, text = "Would you like to encrypt or decrypt a statement?\n\n\n") 
Question.grid(row = 0, column = 1) 
ButtonPlace1 = Button(frame1, text = "Encrypt", command = EncryptChosen) 
ButtonPlace2 = Button(frame1, text = "Decrypt", command = "") 
ButtonPlace1.place(x = 75, y = 50, anchor = "c") 
ButtonPlace2.place(x = 175, y = 50, anchor = "c") 

master.loop() 

这是我的程序代码,它允许您以caeser密码的形式加密或解密语句。到目前为止,我已经对它进行了编码,以便通过它,选择你想要它移动的内容,要加密的内容以及是否要删除空格。我还没有开始解密。但是,在代码中,在收集完所有必要变量后,实际上会对我遇到的问题进行加密。我创建了一个名为“字母表”的字符串,用于存储整个字母表。当函数运行时,我需要找到字母在字母表中的位置,以便我可以移动它。然而,当我已经做到了这一点,并运行它我得到了一个错误:str.find()= TypeError:不支持的操作数类型为+:'int'和'str'..?

position = alphabet.find(character) + shiftKey 
TypeError: unsupported operand type(s) for +: 'int' and 'str' 

此外,当我使用了循环遍历整个语句运行并加密这一切,我会遇到这样的错误:

UnboundLocalError: local variable 'encryptedText' referenced before assignment 

这是一个问题,基本上我需要做的是将每个加密字符添加到加密文本字符串,但这阻止了我这样做。请以任何方式帮助我,因为我需要在2017年3月9日星期一结束此项目。

+1

'alphabet.find(character)'返回字符的位置(整数)。那就是问题所在。 –

+0

我能做些什么来改变它? – Ben2508

+0

还有其他问题呢? – Ben2508

回答

1

alphabet.find(chr)返回一个整数,shiftkey是一个字符串 - 更改一个或另一个匹配。

encrypted_text是其功能中的局部变量 - 您在全局范围顶部的global encrypted_text声明是无用的。 global是为了在函数内部使用,告诉Python列出的变量名不在函数本地,而是在全局范围内找到它。

相关问题