-
Notifications
You must be signed in to change notification settings - Fork 768
Expand file tree
/
Copy pathmorse_code.py
More file actions
135 lines (115 loc) · 3.27 KB
/
morse_code.py
File metadata and controls
135 lines (115 loc) · 3.27 KB
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
#importing modules
from tkinter import *
from tkinter import messagebox as tm
import base64
#initialize window
root = Tk()
root.geometry('500x300')
root.resizable(0,0)
#title of the window
root.title("Morse Code-Message Encode and Decode")
#label
Label(root, text ='ENCODE DECODE', font = 'arial 20 bold').pack()
Label(root, text ='Morse Code', font = 'arial 20 bold').pack(side =BOTTOM)
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':'-----',
}
string=StringVar()
mode=StringVar()
result=StringVar()
def encryptor(string):
encrypted_text=""
for letters in string:
if letters != " ":
sum=str(MORSE_CODE_DICT.get(letters))+" "
encrypted_text+=sum
else:
encrypted_text+=" "
return encrypted_text
#print(encrypted_text)
def decryptor(string):
string+=" "
key_list=list(MORSE_CODE_DICT.keys())
val_list=list(MORSE_CODE_DICT.values())
morse=""
normal=""
for letters in string:
if letters !=" ":
morse+=letters
space_found=0
else:
space_found+=1
if space_found==2:
normal+=" "
else:
normal+=key_list[val_list.index(morse)]
morse=""
return normal
#print(normal)
def Mode():
if mode.get()=='e' or mode.get()=='E':
result.set(encryptor(string.get().upper()))
else:
result.set(decryptor(string.get().upper()))
def Reset():
string.set("")
mode.set("")
result.set("")
def Exit():
choice=tm.askquestion("Exit","Are you sure that you want to exit?")
if choice=="yes":
root.destroy()
#Message
Label(root, font= 'arial 12 bold', text='MESSAGE').place(x= 60,y=60)
Entry(root, font = 'arial 10', textvariable = string, bg = 'ghost white').place(x=290, y = 60)
#key
"""Label(root, font = 'arial 12 bold', text ='KEY').place(x=60, y = 90)
Entry(root, font = 'arial 10', textvariable = private_key , bg ='ghost white').place(x=290, y = 90)"""
#mode
Label(root, font = 'arial 12 bold', text ='MODE(e-encrypt, d-decrypt)').place(x=60, y = 90)
Entry(root, font = 'arial 10', textvariable = mode , bg= 'ghost white').place(x=290, y = 90)
#result
Label(root, font = 'arial 12 bold', text ='RESULT').place(x=60, y = 120)
Entry(root, font = 'arial 10 bold', textvariable = result, bg ='ghost white').place(x=290, y = 120)
#result button
Button(root, font = 'arial 10 bold', text = 'RESULT' ,padx =2,bg ='yellow' ,command = Mode).place(x=300, y = 190)
#reset button
Button(root, font = 'arial 10 bold' ,text ='RESET' ,width =6, command = Reset,bg = 'LimeGreen', padx=2).place(x=100, y = 190)
#exit button
Button(root, font = 'arial 10 bold',text= 'EXIT' , width = 6, command = Exit,bg = 'OrangeRed', padx=2, pady=2).place(x=200, y = 190)
root.mainloop()