-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse_code_translator.py
54 lines (44 loc) · 1.9 KB
/
morse_code_translator.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
import tkinter
from tkinter import *
morse_alphabet = {".-": "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"}
class MyWindow:
def __init__(self, win):
self.lbl1 = Label(win, text='Text in morse')
self.lbl2 = Label(win, text='Translation')
self.t1 = Entry(bd=3)
self.t2 = Entry()
self.btn1 = Button(win, text='Translate!')
self.lbl1.place(x=100, y=50)
self.t1.place(x=200, y=50)
self.b1 = Button(win, text='Translate!', command=self.translate)
self.b1.place(x=100, y=150)
self.lbl2.place(x=100, y=200)
self.t2.place(x=200, y=200)
def translate(self):
self.t2.delete(0, 'end')
text = self.t1.get()
translated_message = []
words_in_morse_to_translate = text.split(" | ")
for word in words_in_morse_to_translate:
current_word = ""
for letter in word.split():
if letter in morse_alphabet:
current_word += morse_alphabet[letter]
translated_message.append(current_word)
self.t2.insert(END, str(' '.join(translated_message).upper()))
window = Tk()
mywin = MyWindow(window)
window.title('Morse translator')
window.geometry("500x500+10+10")
window.mainloop()
# input examples which were taken from the problem description:
# .. | -- .- -.. . | -.-- --- ..- | .-- .-. .. - . | .- | .-.. --- -. --. | -.-. --- -.. .
# this should result in the output I MADE YOU WRITE A LONG CODE
# input
# .. | .... --- .--. . | -.-- --- ..- | .- .-. . | -. --- - | -- .- -..
# output
# I HOPE YOU ARE NOT MAD