A Python-based Morse Code translator and player built using the customtkinter library for the GUI and pygame for audio playback. This application allows users to convert text between English and Morse Code, play Morse Code as audio, and clear/reset inputs with ease.
- Bidirectional Translation: Convert text from English to Morse Code and vice versa.
- Audio Playback: Play Morse Code translations as audio using
.mp3files for dots (dot.mp3) and dashes (dash.mp3). - Interactive GUI: A user-friendly interface built with
customtkinterthat includes:- Input and output text areas.
- Radio buttons to toggle between translation modes (English โ Morse / Morse โ English).
- Buttons for conversion, playback, clearing, and quitting.
- Morse Code Guide: View a visual guide for Morse Code symbols via a pop-up window.
- Python 3.x installed on your system.
- Basic knowledge of running Python scripts.
-
Clone the repository:
git clone https://github.com/Poyamohamadi/morse_code.git cd morse_code -
Install the required dependencies:
pip install customtkinter pygame pillow
-
Place the required audio and image files in the project directory:
dot.mp3: Sound for a dot (short beep).dash.mp3: Sound for a dash (long beep).morse.png: Image file for the Morse Code guide.
-
Run the application:
python main.py
- Launch the application by running the script.
- Enter text in the input box:
- For English โ Morse Code, select the corresponding radio button.
- For Morse Code โ English, select the other radio button.
- Click the "Convert" button to translate the text.
- Use the "Play Morse" button to hear the Morse Code translation as audio.
- Clear the input/output fields using the "Clear" button.
- Exit the application by clicking the "Quit" button.
Note: Ensure that the dot.mp3, dash.mp3, and morse.png files are present in the working directory for proper functionality.
The project is organized into a single Python script (main.py) with a modular structure. Below is a detailed breakdown of the code and its components:
customtkinter: A modern GUI library built on top of Tkinter, used for creating the application's user interface.pygame.mixer: Handles audio playback for Morse Code sounds (dots and dashes).PIL.Image: Used to load and display the Morse Code guide image.os: Provides utilities for interacting with the file system (e.g., loading audio and image files).
The main application class inherits from CTk, which is the base class for customtkinter applications. It encapsulates all the functionality of the Morse Code translator.
-
Initialization (
__init__):- Sets up the application window and calls the
run()method to initialize the GUI.
- Sets up the application window and calls the
-
run()Method:- Configures the main application window (title, size, resizability).
- Defines dictionaries for English-to-Morse and Morse-to-English translations.
- Creates the layout using frames, textboxes, radio buttons, and buttons for user interaction.
-
GUI Layout:
- Input Frame:
- Contains a
CTkTextboxfor user input. - Includes radio buttons to toggle between "English โ Morse" and "Morse โ English" modes.
- A "Guide" button opens a pop-up window displaying the Morse Code guide.
- Contains a
- Output Frame:
- Contains a
CTkTextboxto display the translated output. - Buttons for converting text, playing Morse Code as audio, clearing fields, and quitting the application.
- Contains a
- Input Frame:
-
Helper Methods:
show_guide():- Opens a new window (
CTkToplevel) displaying the Morse Code guide image (morse.png).
- Opens a new window (
get_morse():- Converts English text to Morse Code using the predefined dictionary.
- Filters out invalid characters and formats the output with spaces and separators (
|).
get_english():- Converts Morse Code back to English using the reverse dictionary.
- Handles spaces and separators to reconstruct words.
convert():- Determines the translation direction based on the selected radio button and calls the appropriate conversion method.
play():- Plays the Morse Code translation as audio using
pygame.mixer. - Uses
dot.mp3for dots anddash.mp3for dashes, with pauses for spaces and word separators.
- Plays the Morse Code translation as audio using
clear():- Clears both the input and output textboxes.
- The script checks if it is being run directly (
if __name__ == "__main__":) and initializes theMyAppclass to start the application.
The project is organized as follows:
morse_code/
โโโ main.py # Main application script
โโโ README.md # Project documentation
โโโ dot.mp3 # Audio file for dots
โโโ dash.mp3 # Audio file for dashes
โโโ demo.gif # Gif documention
โโโ morse.png # Morse Code guide image
This project relies on the following Python libraries:
customtkinter: Modern GUI toolkit based on Tkinter.pygame: Used for playing audio files.Pillow: Handles image loading and display.
Contributions are welcome! If you'd like to improve this project, feel free to:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes and push them to your fork.
- Submit a pull request detailing your changes.
Please ensure your code adheres to the existing style and includes appropriate documentation.
This project is licensed under the MIT License. See the LICENSE file for details.
-
CustomTkinter Library: Thanks to the developers of
customtkinterfor creating a modern and customizable GUI toolkit. -
Python Community: Special thanks to the Python community for their support and resources.
For questions or feedback, feel free to reach out:
- GitHub: Poyamohamadi
Thank you for using Morse Code Translator! ๐
