2017-03-09 23 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 cypherの形式で文を暗号化または復号化できるプログラムのコードです。これまでのところ、私はあなたがそれを通過し、それをシフトさせたいもの、暗号化したいもの、スペースを削除したいかどうかを選択するようにコード化しました。私はまだ解読に取り掛かっていません。しかし、必要なすべての変数が収集された後、実際に問題が発生した文を実際に暗号化するコードでは、アルファベット全体を格納する「アルファベット」という文字列を作成しました。関数が実行されるとき、私はそれをシフトすることができるように、文字がアルファベットのどこにあるかを見つける必要があります。私は文全体を介して実行し、それをすべて暗号化するforループを使用した場合、私はこのエラーが発生し、また、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 
私はこれをやって、それを走っていたときしかし、私はエラーを得ました

これは本質的に私がする必要があるのは、暗号化された文字列にそれぞれの暗号化された文字を追加することで問題になりますが、

+1

'alphabet.find(character)'は、文字の位置(整数)を返します。それが問題です。 –

+0

これを変更するにはどうすればよいですか? – Ben2508

+0

また、他の問題はどうですか? – Ben2508

答えて

1

alphabet.find(chr)整数を返します。shiftkeyは、一致する文字列 - 1つまたはもう一方の文字列です。

encrypted_textは、その関数のローカル変数です。グローバルスコープの先頭にあるglobal encrypted_text宣言は無用です。 globalは、関数内で使用され、リストされた変数名が関数に対してローカルではないことをPythonに伝え、グローバルスコープでそれを見つけることを意味します。

関連する問題