- CodeCraft by Dr. Christine Lee
- Posts
- Secure Your Secrets: Store Cipher Text Now! ๐๐
Secure Your Secrets: Store Cipher Text Now! ๐๐
A Fun Caesar Cipher Text App with PySimpleGUI!
In this post, weโre taking your Caesar Cipher app to the next level by adding a feature to save your encoded or decoded messages to a file.
This way, you can easily keep track of your secret messages or share them with others later.
Not only is this a fun way to reinforce your learning, but it also introduces you to file handling in Python, a fundamental skill for any budding programmer!
First version of the Caesar Cipher App is posted here.
Step-by-Step Guide to Save Your Message
1. Add a Save Button:
Letโs start by adding a "Save" button to your GUI.
This button will trigger the function that saves your encoded or decoded message to a file.
2. Define the Save Function:
You'll need to write a function that will open a file dialog, allowing the user to choose where to save the file.
The file will be saved in .txt
format by default.
3. Update the Event Loop:
Modify your event loop to include the action for the "Save" button. When clicked, the button will call the save function and write the message to the file.
Hereโs a sample code snippet to illustrate:
import PySimpleGUI as sg
# Layout with Save button added
layout = [
[sg.Text('Enter Shift Key'), sg.InputText(key='-KEY-')],
[sg.Text('Enter Text'), sg.InputText(key='-TEXT-')],
[sg.Button('Encode'), sg.Button('Decode')],
[sg.Text('Result'), sg.InputText(key='-RESULT-')],
[sg.Button('Save'), sg.Button('Exit')]
]
# Window
window = sg.Window('Caesar Cipher', layout)
def caesar_cipher(text, shift, mode='encode'):
shift = int(shift)
if mode == 'decode':
shift = -shift
result = ''.join([chr((ord(char) - 65 + shift) % 26 + 65) if char.isupper()
else chr((ord(char) - 97 + shift) % 26 + 97) if char.islower()
else char for char in text])
return result
def save_to_file(text):
file_path = sg.popup_get_file('Save As', save_as=True, no_window=True, default_extension='.txt', file_types=(("Text Files", "*.txt"),))
if file_path:
with open(file_path, 'w') as file:
file.write(text)
sg.popup('Message Saved!', 'Your message was successfully saved.')
# Event Loop
while True:
event, values = window.read()
if event sg.WINDOW_CLOSED or event 'Exit':
break
elif event == 'Encode':
result = caesar_cipher(values['-TEXT-'], values['-KEY-'], 'encode')
window['-RESULT-'].update(result)
elif event == 'Decode':
result = caesar_cipher(values['-TEXT-'], values['-KEY-'], 'decode')
window['-RESULT-'].update(result)
elif event == 'Save':
save_to_file(values['-RESULT-'])
window.close()
Explanation for the save_to_file()
Letโs learn more about the save_to_file
function:
1. Defining the Function:
The function is named save_to_file
, and it takes one argument, text
, which is the message you want to save.
2. Getting the File Path:
The function uses sg.popup_get_file
to open a dialog window where you can choose where to save your file.
The dialog allows you to save the file with a specific name and location.
The save_as=True
parameter ensures that you can create a new file, and no_window=True
makes the dialog appear without a background window.
The default file extension is set to .txt
, so it saves as a text file unless you choose another extension.
3. Checking the Path:
The function checks if the user selected a file path. If they did, the path is stored in file_path
.
4. Writing to the File:
If the file path is valid, the function opens the file at that path in write mode (`'w'`).
Then, it writes the text
content into the file.
5. Confirmation Popup:
After successfully saving the file, a popup message appears, telling the user that the message was saved.
Output
The Layout
Entering the Data
Save Data into a Text File
Data is Saved
File Content
What Youโve Learned
In this post, youโve added a file-saving feature to your Caesar Cipher app.
You now know how to use PySimpleGUI to create a dialog box for saving files and how to manage file writing in Python.
This skill will be incredibly useful as you continue to build more sophisticated applications!
Coding with a Smile ๐คฃ ๐
String Formatting Follies: Getting the hang of string formatting is like learning to bake. You start with simple recipes and eventually create elaborate, perfectly frosted cakes (or beautifully formatted strings).
Letโs Inspire Future AI Coders Together! โ
Iโm excited to continue sharing my passion for Python programming and AI with you all. If youโve enjoyed the content and found it helpful, do consider supporting my work with a small gift. Just click the link below to make a difference โ itโs quick, easy, and every bit helps and motivates me to keep creating awesome contents for you.
Thank you for being amazing!
Whatโs Next? ๐
Stay tuned for our next exciting post, where weโll dive into more advanced GUI features! Happy coding! ๐
Ready for More Python Fun? ๐ฌ
Subscribe to our newsletter now and get a free Python cheat sheet! ๐ Dive deeper into Python programming with more exciting projects and tutorials designed just for beginners.
Keep learning, keep coding ๐ฉโ๐ป๐จโ๐ป, and keep discovering new possibilities! ๐ปโจ
Enjoy your journey into artificial intelligence, machine learning, data analytics, data science and more with Python!
Stay tuned for our next exciting project in the following edition!
Happy coding!๐๐โจ
๐ We want to hear from you! ๐ How do you feel about our latest newsletter? Your feedback will help us make it even more awesome! |