Message Encode-Decode using Python Tkinter


In this article, the user will understand how to encode-decode messages in python. Users can enter a message and select the desired option whether to encrypt or decode it. The encode or decoded message would be depicted on GUI window after clicking on the respective button. The Tkinter module would be used to develop interactive GUI applications. The code creates a straightforward GUI window with an input entry field, two encoding and decoding buttons, and an output label to show the outcome.

Message Encode-Decode Using Python Tkinter

Code Explanation and Design Steps −

  • Step 1 − Open Jupyter Notebook in Anaconda prompt and start writing the code in its cell.

  • Step 2 − Import ‘Tkinter’ module and alias it as tk.

  • Step 3 − In the function ‘encode_message()’

    • Using the ‘entry.get()’ method, we can get the text entered into the input entry field. The message variable holds the entered message. We use slicing and the ‘[::-1]’ syntax to reverse the characters in the message variable in order to encrypt the message. The encoded message is produced in this and saved in the ‘encoded_message’ variable. The output label's text can be changed using the ‘output_label.config(text=...)’ function. Here, "Encoded Message:" and the “encoded_message” are concatenated into the label's text.

  • Step 4 − In the ‘decode_message()’ method, similarly −

    • Using ‘entry.get()’, we are able to obtain the encoded message from the input entry field. The encoded_message variable holds the message that has been encrypted. We use slicing with ‘[::-1]’ to reverse the characters in the ‘encoded_message’ variable in order to decode the message. The ‘decoded_message’ variable, which is retrieved via this, holds the original message. The output label's text can be changed using the ‘output_label.config(text=...)’ function. Here, "Decoded Message:" and the decoded_message are concatenated into the label's content.

  • Step 5 − After that, we use ‘tk.Tk()’ to build the application's main window. The GUI's root window is initialized by doing this. The window's title is set to "Message Encode Decode" using the title() method. The window's dimensions are specified using the ‘geometry()’ method. It is now set to "300x200" pixels.

  • Step 6 − Using ‘tk.Entry()’, we create an entry field to accept user input. The user can enter the message to be encoded or decoded here. Using the ‘width’ parameter, we defined the entry field's width as 30 characters. The entry field is packed into the main window using the ‘entry.pack()’ method. This describes where it should be in the window and how it should look.

  • Step 7‘Tk.Button()’ function is used to construct the buttons "Encode" and "Decode". The user can utilize these buttons to start the related encode and decode Message Encode-Decode using Python Tkinter processes. The text option is used to customize the button wording. The ‘encode_message()’ function and the decode_message() function are connected to the "Encode" and "Decode" buttons, respectively, via the command argument. The buttons are arranged in the main window using the ‘button.pack()’ method. Their positioning and appearance in the window are determined by this.

  • Step 8 − Using ‘tk.Label()’, an output label is generated. The message will be presented here, whether it has been encoded or decoded. The output label is packed into the main window using the ‘output_label.pack()’ function. This describes where it should be in the window and how it should look.

  • Step 9 − Finally, use ‘window.mainloop()’ to launch the main event loop. This method starts the Tkinter event loop, which watches for user interactions like button clicks and text inputs continuously. The GUI is accordingly updated by the event loop. Check the results.

Example

Code for Message Encode-decode Using Python Tkinter −

import tkinter as tk

def encode_message():
   message = entry.get()
   encoded_message = message[::-1]
   output_label.config(text="Encoded Message: " + encoded_message)

def decode_message():
   encoded_message = entry.get()
   decoded_message = encoded_message[::-1]
   output_label.config(text="Decoded Message: " + decoded_message)
# Creation of the main window
window = tk.Tk()
window.title("Message Encode-Decode")
window.geometry("300x200")
# Create input entry
entry = tk.Entry(window, width=30)
entry.pack(pady=20)

# Create encode button
encode_button = tk.Button(window, text="Encode", command=encode_message)
encode_button.pack()

# Creation of decode button
decode_button = tk.Button(window, text="Decode", command=decode_message)
decode_button.pack()

# Creation of output label
output_label = tk.Label(window, text="")
output_label.pack(pady=20)

# Start the main tkinter event loop
window.mainloop()

Output

Output results can be seen on GUI-based window for message encode-decode using Tkinter by clicking on its buttons such as encode and decode.

Conclusion

In this article, (GUI) the based window comprises two buttons Encode and decode, and an output label will be demonstrated when executing this code. Users may enter the message in the input area, then any of the buttons to see the outcome appear in the output label.

Updated on: 18-Oct-2023

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements