|
| 1 | +# Import Tkinter module |
| 2 | +from tkinter import * |
| 3 | +from tkinter import messagebox |
| 4 | + |
| 5 | +# Create a window |
| 6 | +root = Tk() |
| 7 | + |
| 8 | +# Create global variables |
| 9 | +variable1 = StringVar(root) |
| 10 | +variable2 = StringVar(root) |
| 11 | + |
| 12 | +# Initialise the variables |
| 13 | +variable1.set("Language select") |
| 14 | +variable2.set("Language select") |
| 15 | + |
| 16 | +""" |
| 17 | +VARIABLE KEY |
| 18 | +'cipher' -> 'stores the morse translated form of the english string' |
| 19 | +'decipher' -> 'stores the english translated form of the morse string' |
| 20 | +'citext' -> 'stores morse code of a single character' |
| 21 | +'i' -> 'keeps count of the spaces between morse characters' |
| 22 | +'message' -> 'stores the string to be encoded or decoded' |
| 23 | +""" |
| 24 | + |
| 25 | +# Dictionary representing the morse code chart |
| 26 | +MORSE_CODE_DICT = { |
| 27 | + "A": ".-", |
| 28 | + "B": "-...", |
| 29 | + "C": "-.-.", |
| 30 | + "D": "-..", |
| 31 | + "E": ".", |
| 32 | + "F": "..-.", |
| 33 | + "G": "--.", |
| 34 | + "H": "....", |
| 35 | + "I": "..", |
| 36 | + "J": ".---", |
| 37 | + "K": "-.-", |
| 38 | + "L": ".-..", |
| 39 | + "M": "--", |
| 40 | + "N": "-.", |
| 41 | + "O": "---", |
| 42 | + "P": ".--.", |
| 43 | + "Q": "--.-", |
| 44 | + "R": ".-.", |
| 45 | + "S": "...", |
| 46 | + "T": "-", |
| 47 | + "U": "..-", |
| 48 | + "V": "...-", |
| 49 | + "W": ".--", |
| 50 | + "X": "-..-", |
| 51 | + "Y": "-.--", |
| 52 | + "Z": "--..", |
| 53 | + "1": ".----", |
| 54 | + "2": "..---", |
| 55 | + "3": "...--", |
| 56 | + "4": "....-", |
| 57 | + "5": ".....", |
| 58 | + "6": "-....", |
| 59 | + "7": "--...", |
| 60 | + "8": "---..", |
| 61 | + "9": "----.", |
| 62 | + "0": "-----", |
| 63 | + ", ": "--..--", |
| 64 | + ".": ".-.-.-", |
| 65 | + "?": "..--..", |
| 66 | + "/": "-..-.", |
| 67 | + "-": "-....-", |
| 68 | + "(": "-.--.", |
| 69 | + ")": "-.--.-", |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +# Function to clear both the text areas |
| 74 | +def clearAll(): |
| 75 | + # whole content of text area is deleted |
| 76 | + language1_field.delete(1.0, END) |
| 77 | + language2_field.delete(1.0, END) |
| 78 | + |
| 79 | + |
| 80 | +# Function to perform conversion from one |
| 81 | +# language to another |
| 82 | +def convert(): |
| 83 | + # get a whole input content from text box |
| 84 | + # ignoring \n from the text box content |
| 85 | + message = language1_field.get("1.0", "end")[:-1] |
| 86 | + |
| 87 | + # get the content from variable1 and 2, check their values |
| 88 | + if variable1.get() == variable2.get(): |
| 89 | + # show the error message |
| 90 | + messagebox.showerror("Can't Be same Language") |
| 91 | + return |
| 92 | + |
| 93 | + elif variable1.get() == "English" and variable2.get() == "Morse Code": |
| 94 | + # function call |
| 95 | + rslt = encrypt(message) |
| 96 | + |
| 97 | + elif variable1.get() == "Morse Code" and variable2.get() == "English": |
| 98 | + # function call |
| 99 | + rslt = decrypt(message) |
| 100 | + |
| 101 | + else: |
| 102 | + # show the error message |
| 103 | + messagebox.showerror("please choose valid language code..") |
| 104 | + return |
| 105 | + |
| 106 | + # insert content into text area |
| 107 | + # from rslt variable |
| 108 | + language2_field.delete(1.0, END) |
| 109 | + language2_field.insert("end -1 chars", rslt) |
| 110 | + |
| 111 | + |
| 112 | +# Function to encrypt the string |
| 113 | +# according to the morse code chart |
| 114 | +def encrypt(message): |
| 115 | + cipher = "" |
| 116 | + for letter in message: |
| 117 | + if letter.isalpha(): |
| 118 | + cipher += MORSE_CODE_DICT[letter.upper()] + " " |
| 119 | + |
| 120 | + elif letter != " ": |
| 121 | + # Looks up the dictionary and adds the |
| 122 | + # corresponding morse code |
| 123 | + # along with a space to separate |
| 124 | + # morse codes for different characters |
| 125 | + cipher += MORSE_CODE_DICT[letter] + " " |
| 126 | + else: |
| 127 | + # 1 space indicates different characters |
| 128 | + # and 2 indicates different words |
| 129 | + cipher += " " |
| 130 | + |
| 131 | + return cipher |
| 132 | + |
| 133 | + |
| 134 | +# Function to decrypt the string |
| 135 | +# from morse to english |
| 136 | +def decrypt(message): |
| 137 | + # extra space added at the end to access the |
| 138 | + # last morse code |
| 139 | + message += " " |
| 140 | + |
| 141 | + decipher = "" |
| 142 | + citext = "" |
| 143 | + for letter in message: |
| 144 | + # checks for space |
| 145 | + if letter != " ": |
| 146 | + # counter to keep track of space |
| 147 | + i = 0 |
| 148 | + |
| 149 | + # storing morse code of a single character |
| 150 | + citext += letter |
| 151 | + |
| 152 | + # in case of space |
| 153 | + else: |
| 154 | + # if i = 1 that indicates a new character |
| 155 | + i += 1 |
| 156 | + |
| 157 | + # if i = 2 that indicates a new word |
| 158 | + if i == 2: |
| 159 | + # adding space to separate words |
| 160 | + decipher += " " |
| 161 | + else: |
| 162 | + # accessing the keys using their values |
| 163 | + # (reverse of encryption) |
| 164 | + decipher += list(MORSE_CODE_DICT.keys())[ |
| 165 | + list(MORSE_CODE_DICT.values()).index(citext) |
| 166 | + ] |
| 167 | + citext = "" |
| 168 | + |
| 169 | + return decipher |
| 170 | + |
| 171 | + |
| 172 | +# Driver code |
| 173 | +if __name__ == "__main__": |
| 174 | + # Set the background colour of GUI window |
| 175 | + root.configure() |
| 176 | + |
| 177 | + # Set the configuration of GUI window (WidthxHeight) |
| 178 | + root.geometry("450x400") |
| 179 | + |
| 180 | + # set the name of tkinter GUI window |
| 181 | + root.title("Morse Code Translator") |
| 182 | + |
| 183 | + # Create Welcome to Morse Code Translator label |
| 184 | + headlabel = Label( |
| 185 | + root, text="Welcome to Morse Code Translator", fg="black", justify="center" |
| 186 | + ) |
| 187 | + |
| 188 | + # # Create a "One Language " label |
| 189 | + label1 = Label(root, text="First Language : Input", fg="black") |
| 190 | + |
| 191 | + # # Create a "From Language " label |
| 192 | + label2 = Label(root, text="Second Language : To Convert", fg="black") |
| 193 | + |
| 194 | + # # Create a "To Language " label |
| 195 | + # label3 = Label(root, text="To Language ", fg="black", bg="dark green") |
| 196 | + |
| 197 | + # # Create a "Converted Language " label |
| 198 | + # label4 = Label(root, text="Converted Language ", fg="black", bg="dark green") |
| 199 | + |
| 200 | + # grid method is used for placing |
| 201 | + # the widgets at respective positions |
| 202 | + # in table like structure . |
| 203 | + headlabel.grid(row=0, column=1) |
| 204 | + label1.grid(row=2, column=0) |
| 205 | + label2.grid(row=3, column=0) |
| 206 | + # label3.grid(row=3, column=0) |
| 207 | + # label4.grid(row=5, column=0) |
| 208 | + |
| 209 | + # Create a text area box |
| 210 | + # for filling or typing the information. |
| 211 | + language1_field = Text(root, height=5, width=25, font="lucida 13") |
| 212 | + language2_field = Text(root, height=5, width=25, font="lucida 13") |
| 213 | + |
| 214 | + # padx keyword argument used to set padding along x-axis . |
| 215 | + language1_field.grid(row=1, column=1, padx=10) |
| 216 | + language2_field.grid(row=5, column=1, padx=10) |
| 217 | + |
| 218 | + # list of language codes |
| 219 | + languageCode_list = ["English", "Morse Code"] |
| 220 | + |
| 221 | + # create a drop down menu using OptionMenu function |
| 222 | + # which takes window name, variable and choices as |
| 223 | + # an argument. use * before the name of the list, |
| 224 | + # to unpack the values |
| 225 | + FromLanguage_option = OptionMenu(root, variable1, *languageCode_list) |
| 226 | + ToLanguage_option = OptionMenu(root, variable2, *languageCode_list) |
| 227 | + |
| 228 | + FromLanguage_option.grid(row=2, column=1, ipadx=10) |
| 229 | + ToLanguage_option.grid(row=3, column=1, ipadx=10) |
| 230 | + |
| 231 | + # Create a Convert Button and attached |
| 232 | + # with convert function |
| 233 | + button1 = Button( |
| 234 | + root, text="Convert", bg="gray", fg="black", command=convert, cursor="hand2" |
| 235 | + ) |
| 236 | + |
| 237 | + button1.grid(row=4, column=1) |
| 238 | + |
| 239 | + # Create a Clear Button and attached |
| 240 | + # with clearAll function |
| 241 | + button2 = Button( |
| 242 | + root, |
| 243 | + text="Clear", |
| 244 | + bg="red", |
| 245 | + fg="black", |
| 246 | + command=clearAll, |
| 247 | + padx=10, |
| 248 | + cursor="hand2", |
| 249 | + ) |
| 250 | + |
| 251 | + button2.grid(row=6, column=1) |
| 252 | + |
| 253 | + # Start the GUI |
| 254 | + root.mainloop() |
0 commit comments