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