-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse-code.py
176 lines (156 loc) · 4.83 KB
/
morse-code.py
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
# import packages
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import winsound, time, io, pygame
from gtts import gTTS
import tkinter as Tk
from tkinter import PhotoImage, StringVar, messagebox
try:
import winsound
except ImportError:
import os
def playSound(frequency,duration):
#apt-get install beep
os.system(f'beep -f {frequency} -l {duration}')
else:
def playSound(frequency,duration):
winsound.Beep(frequency,duration)
# Morse code dictionary
morse_code: "dict[str, str]" = {
"": " ",
" ": "/",
"\n": "//",
"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": "--..",
}
# inverse dictionary
textConverter: "dict[str, str]" = {value: key for (key, value) in morse_code.items()}
# Variables
root: Tk = Tk.Tk()
root.geometry("820x300")
root.resizable(0,0)
root.title("Morse Code")
icon = PhotoImage(file = './icon/morse-code.png')
root.iconphoto(False, icon)
text_var: StringVar = Tk.StringVar()
code_var: StringVar = Tk.StringVar()
# Text to code
def encoder() -> None:
code_display.config(state="normal")
code_display.delete("1.0", Tk.END)
text: str = text_entry.get("1.0", "end-1c").upper()
code: str = ""
try:
for letter in text:
code = f"{code} {morse_code[letter]}"
except Exception:
messagebox.showerror("Error", "Please enter valid text")
code_display.insert(Tk.INSERT, code)
code_display.config(state="disabled")
# Code to text
def decoder():
text_display.config(state="normal")
text_display.delete("1.0", Tk.END)
code: str = code_entry.get("1.0", "end-1c")
groups: list[str] = [i for j in code.split() for i in (j, " ")]
text: str = ""
try:
for group in groups:
text = text + textConverter[group]
except Exception:
messagebox.showerror("Error", "Please enter valid code")
text_display.insert(Tk.INSERT, text)
text_display.config(state="disabled")
# Code to beeps
def sound():
code: str = code_display.get("1.0", "end-1c")
beeps: list = []
beeps[1:]: list = code
for beep in beeps:
if beep == ".":
# winsound.Beep(1000, 200)
playSound(1000,200)
elif beep == "-":
# winsound.Beep(1000, 600)
playSound(1000,600)
elif beep in ["/", "//"]:
time.sleep(0.8)
else:
time.sleep(0.6)
# Text to audio
def speak():
text: str = text_display.get("1.0", "end-1c")
try:
with io.BytesIO() as file:
gTTS(text=text, lang="en").write_to_fp(file)
file.seek(0)
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
except Exception:
messagebox.showwarning("Sorry", "I only speak english :(")
# Main function
if __name__ == "__main__":
text_label = Tk.Label(root, text="Enter text : ")
text_entry = Tk.Text(root, width=50, height=5)
code_output = Tk.Label(root, text="Encoded message : ")
code_display = Tk.Text(root, width=50, height=5)
code_label = Tk.Label(root, text="Enter code : ")
code_entry = Tk.Text(root, width=50, height=5)
text_output = Tk.Label(root, text="Decoded message : ")
text_display = Tk.Text(root, width=50, height=5)
text_btn = Tk.Button(root, text="Encode", command=encoder, width=50)
sound_btn = Tk.Button(root, text="Beep", command=sound, width=50)
code_btn = Tk.Button(root, text="Decode", command=decoder, width=50)
speak_btn = Tk.Button(root, text="Audio", command=speak, width=50)
text_label.grid(row=0, column=0, padx=2, pady=2)
text_entry.grid(row=1, column=0, padx=2, pady=2)
text_btn.grid(row=2, column=0, padx=2, pady=2)
code_output.grid(row=3, column=0, padx=2, pady=2)
code_display.grid(row=4, column=0, padx=2, pady=2)
sound_btn.grid(row=5, column=0, padx=2, pady=2)
code_label.grid(row=0, column=1, padx=2, pady=2)
code_entry.grid(row=1, column=1, padx=2, pady=2)
code_btn.grid(row=2, column=1, padx=2, pady=2)
text_output.grid(row=3, column=1, padx=2, pady=2)
text_display.grid(row=4, column=1, padx=2, pady=2)
speak_btn.grid(row=5, column=1, padx=2, pady=2)
code_display.config(state="disabled")
text_display.config(state="disabled")
root.mainloop()