Loading

Welcome back to the second part of our three-part article series on building a Python PyQt5 application that converts text to Morse Code. In this part, we will dive deeper into the history of Morse Code and how it has evolved over the years, and then we will discuss how we can use Python and PyQt5 to create a user-friendly application that converts any text to Morse Code.

Before we begin, let’s briefly recap the previous article. In the first part of this series, we discussed what Morse Code is and how it works. We also reviewed the basic structure of a Python console application and created a simple program that accepts user input and converts it to Morse Code. This article will build on that foundation and create a graphical user interface using PyQt5 to make the program more user-friendly.

History of Morse Code

Samuel Morse invented Morse Code in the early 1830s to communicate over long distances using electrical impulses. It was first used in telegraphy and quickly became the standard for long-distance communication. It was the primary form of communication for many years before the advent of the radio and telephone.

Over the years, Morse Code has evolved and adapted to meet the changing needs of communication technology. One of the most significant changes occurred in 2007 when the International Morse Code was revised to include new characters, such as ‘@’ and ‘#’, commonly used in modern communication.

Creating a PyQt5 Application to Convert Text to Morse Code

Now, let’s move on to the main topic of this article: creating a PyQt5 application that converts text to Morse Code. To get started, make sure you have PyQt5 installed on your computer. You can do this by running the following command in your terminal:

Once you have PyQt5 installed, open up your favorite text editor and create a new Python file. We’ll start by importing the necessary libraries and setting up the basic structure of our application:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QVBoxLayout

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': '--..', '0': '-----',
    '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....',
    '7': '--...', '8': '---..', '9': '----.'
}

class MorseCodeConverter(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.label = QLabel('Enter text to convert to Morse Code:')
        self.text_input = QLineEdit()
        self.text_output = QLabel()

        # Set up the layout
        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.text_input)
        layout.addWidget(self.text_output)

        # Set up the signal and slot for the text input
        self.text_input.textChanged.connect(self.convert_to_morse)

        self.setLayout(layout)
        self.setWindowTitle('Morse Code Converter')
        self.show()

    def convert_to_morse(self):
        text = self.text_input.text().upper()
        morse_text = ''
        for char in text:
            if char in morse_code_dict:
                morse_text += morse_code_dict[char] + ' '
            else:
                morse_text += char + ' '
        self.text_output.setText(morse_text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    converter = MorseCodeConverter()
    sys.exit(app.exec_())

Let’s go through the code line by line:

  • We import the necessary libraries: `sys` for system-related functionality, and `QApplication`, `QWidget`, `QLabel`, `QLineEdit`, and `QPushButton` from `PyQt5.QtWidgets` for building the graphical user interface.
  • We create a new class `MorseCodeConverter` that inherits from `QWidget`.
  • In the `__init__` method, we call the `initUI` method to set up the user interface.
  • In the `initUI` method, we create a title, size, and layout window. We also make a label to display the input field, an input field for the user to enter text, a label to display the output field, an output field to display the converted Morse Code, and a button to convert the input text to Morse Code. We set the positions and sizes of each element using the `move` and `resize` methods and connect the `clicked` signal of the button to the `convert_text` method.
  • In the `convert_text` method, we get the user input from the input field, convert it to Morse Code using the `to_morse_code` method, and display the Morse Code in the output field.
  • In the `to_morse_code` method, we define a dictionary that maps each character to its corresponding Morse Code representation. We then iterate over the input text, converting each character to Morse Code using the dictionary and appending it to the `morse_code` variable. If the character is not in the dictionary (e.g. a special character), we append a `/` to represent a space

Conclusion

This article covered the development of a PyQT5 Morse Code Converter application. We walked through the code, explaining each line and how it works. Additionally, we explored the history of Morse Code and its essential role in communication technology.

With this application, you can quickly and easily convert any text into Morse Code, allowing you to communicate in this classic and historic form of communication. Try it out for yourself and see how much fun it can be to use Morse Code!

In this series’ third and final part, we will add sound to this project. Stay tuned, it should be interesting.

Add Comment

Your email address will not be published. Required fields are marked *