-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.py
142 lines (122 loc) · 2.68 KB
/
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
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
import tkinter as tk
import winsound
from tkinter import *
root = tk.Tk()
root.title("Translator Morse")
root.geometry("460x450")
img = PhotoImage(file='images/morse.png')
root.tk.call('wm', 'iconphoto', root._w, img)
letter = tk.StringVar()
morse = tk.StringVar()
alphabet_morse = {
"a": ".-/",
"b": "-.../",
"c": "-.-./",
"d": "-../",
"e": "./",
"f": "..-./",
"g": "--./",
"h": "..../",
"i": "../",
"j": ".---/",
"k": "-.-/",
"l": ".-../",
"m": "--/",
"n": "-./",
"o": "---/",
"p": ".--./",
"q": "--.-/",
"r": ".-./",
"s": ".../",
"t": "-/",
"u": "..-/",
"v": "...-/",
"w": ".--/",
"y": "-..-/",
"x": "-.--/",
"z": "--../",
" ": "/"
}
letter_to_morse = {
".-": "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",
}
def translate(letter):
try:
inp = letter
res = ""
for part in inp:
res += alphabet_morse[part]
return res
except KeyError:
return "Insert a valid string"
def translate_morse(message):
try:
inp = message
res = ""
for part in inp.split("/"):
res += letter_to_morse[part]
print(res)
return res
except KeyError:
return "Insert a valid morse"
def play(morse):
for x in morse:
if x == '.':
winsound.Beep(3799, 500)
elif x == '-':
winsound.Beep(3799, 1000)
else:
continue
def button1():
global tt
tt = translate(letter.get())
entrytt = Entry(root, width=50, bg="#f1f1f1", borderwidth=0)
entrytt.insert(0, "The text in morse is: " + str(tt))
entrytt.grid(row=2, column=0)
def button2():
tm = translate_morse(morse.get())
entrytm = Entry(root, width=50, bg="#f1f1f1", borderwidth=0)
entrytm.insert(0, "Morse in text is: " + tm)
entrytm.grid(row=5, column=0)
mylabel1 = Label(root, text="Note: Write a clean text, without commas or dots", fg="#ff0000")
mylabel1.grid(row=0, column=0)
e = Entry(root, textvariable=letter, width=50)
e.grid(row=1, column=0)
mybutton1 = Button(root, text="Translate", command=button1)
mybutton1.grid(row=1, column=1)
mylabel2 = Label(root, text='Note: To separate the letters use "/"', fg="#ff0000")
mylabel2.grid(row=3, column=0)
e1 = Entry(root, textvariable=morse, width=50)
e1.grid(row=4, column=0)
mybutton2 = Button(root, text="Translate", command=button2)
mybutton2.grid(row=4, column=1)
mybutton3 = Button(root, text='Listen', command= lambda: play(tt))
mybutton3.grid(row=1, column=3)
button_exit = Button(root, text="Exit", command=root.quit)
button_exit.grid(row=10, column=0)
root.mainloop()