Skip to content

Commit

Permalink
Merge pull request #27 from aviralgarg05/main
Browse files Browse the repository at this point in the history
Morse Code Translator with GUI
  • Loading branch information
UTSAVS26 authored Oct 2, 2024
2 parents cf4f385 + 8f77abe commit 0b9a4de
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Beginner_Projects/Morse Code Translator with GUI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## **Morse Code Translator GUI**

### 🎯 **Goal**

The main goal of this project is to provide a simple, user-friendly tool to translate text between English and Morse code, facilitating quick and efficient communication for learning, teaching, or decoding purposes.

### 🧵 **Dataset**

No specific dataset is used in this project as it operates based on a predefined dictionary of Morse code mappings for the English alphabet, numerals, and common symbols.

### 🧾 **Description**

This project is a GUI-based application that allows users to easily convert between English and Morse code using a clean and intuitive interface. Built with Python and Tkinter, it provides both encoding (English to Morse) and decoding (Morse to English) functionality, supporting a variety of characters and symbols. The translator is designed for anyone interested in learning Morse code or performing translations between the two languages.

### 🧮 **What I had done!**

1. Designed a simple GUI interface using Tkinter for seamless user interaction.
2. Implemented two-way translation between English text and Morse code.
3. Added functionality to clear the input and output fields.
4. Provided error handling for invalid selections and inputs.
5. Supported a wide range of characters, including letters, numbers, and symbols.
6. Developed logic to handle Morse code input and convert it back to English text.
7. Optimized the process for easy real-time conversion with just one click.

### 🚀 **Models Implemented**

This project does not use machine learning algorithms or models, as the translation is achieved using a predefined dictionary of Morse code. The chosen method of direct mapping between characters and Morse symbols ensures efficiency and accuracy for this specific task.

### 📚 **Libraries Needed**

- `Tkinter` for creating the GUI interface.
- `messagebox` from Tkinter for displaying error messages.

### 📊 **Exploratory Data Analysis Results**

Since the project does not involve a dataset or statistical analysis, there are no EDA results to display.

### 📈 **Performance of the Models based on the Accuracy Scores**

This project does not rely on machine learning models, and hence, there are no accuracy scores to report. The translation between English and Morse code is achieved through direct mapping, ensuring 100% accuracy for valid input.

### 📢 **Conclusion**

This Morse Code Translator GUI offers a simple, efficient tool for translating text between English and Morse code. It serves as an educational tool or utility for decoding and encoding Morse code, ensuring quick conversions. The user-friendly interface makes it accessible to users of all experience levels, and the translation logic ensures reliable and accurate output.

### ✒️ **Your Signature**

**Aviral Garg**
[GitHub](https://github.com/aviralgarg05) | [LinkedIn](https://www.linkedin.com/in/aviralgarg05)
161 changes: 161 additions & 0 deletions Beginner_Projects/Morse Code Translator with GUI/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Import Tkinter module
from tkinter import *
from tkinter import messagebox

# Create a window
root = Tk()

# Create global variables
variable1 = StringVar(root)
variable2 = StringVar(root)

# Initialise the variables
variable1.set("Language select")
variable2.set("Language select")

"""
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": "-----", ", ": "--..--", ".": ".-.-.-", "?": "..--..",
"/": "-..-.", "-": "-....-", "(": "-.--.", ")": "-.--.-", "!": "-.-.--",
"&": ".-...", ":": "---...", ";": "-.-.-.", "=": "-...-", "+": ".-.-.",
"_": "..--.-", "\"": ".-..-.", "$": "...-..-", "@": ".--.-."
}

# 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 conversion 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 Code":
# function call for encryption
rslt = encrypt(message)

elif variable1.get() == "Morse Code" and variable2.get() == "English":
# function call for decryption
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.delete(1.0, END)
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.isalpha():
cipher += MORSE_CODE_DICT[letter.upper()] + " "
elif letter != " ":
# Looks up the dictionary and adds the corresponding morse code
cipher += MORSE_CODE_DICT[letter] + " "
else:
# 1 space indicates different characters, 2 spaces indicate 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
else:
# if i = 1 that indicates a new character
i += 1
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()

# Set the configuration of GUI window (WidthxHeight)
root.geometry("450x400")

# set the name of tkinter GUI window
root.title("Morse Code Translator")

# Create Welcome to Morse Code Translator label
headlabel = Label(root, text="Welcome to Morse Code Translator", fg="black", justify="center")

# Create labels for input and conversion fields
label1 = Label(root, text="First Language : Input", fg="black")
label2 = Label(root, text="Second Language : To Convert", fg="black")

# grid method is used for placing widgets at respective positions
headlabel.grid(row=0, column=1)
label1.grid(row=2, column=0)
label2.grid(row=3, column=0)

# Create text area boxes for input and output
language1_field = Text(root, height=5, width=25, font="lucida 13")
language2_field = Text(root, height=5, width=25, font="lucida 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 Code"]

# create drop down menus for selecting languages
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 buttons for Convert and Clear functionalities
button1 = Button(root, text="Convert", bg="gray", fg="black", command=convert, cursor="hand2")
button1.grid(row=4, column=1)

button2 = Button(root, text="Clear", bg="red", fg="black", command=clearAll, padx=10, cursor="hand2")
button2.grid(row=6, column=1)

# Start the GUI
root.mainloop()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ The PyVerse repository is organized as follows:
│ └── stack.py
├── Automation_Tools
├── Beginner_Projects
│ ├── Morse Code Translator with GUI
│ │ ├── README.md
│ │ ├── main.py
│ │ └── screenshots
│ │ └── tkinter-working.gif
│ └── Stock App
│ ├── Readme.md
│ ├── Templates
Expand Down
5 changes: 5 additions & 0 deletions repo_structure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
│ └── stack.py
├── Automation_Tools
├── Beginner_Projects
│ ├── Morse Code Translator with GUI
│ │ ├── README.md
│ │ ├── main.py
│ │ └── screenshots
│ │ └── tkinter-working.gif
│ └── Stock App
│ ├── Readme.md
│ ├── Templates
Expand Down

0 comments on commit 0b9a4de

Please sign in to comment.