Build Your Own Encryption Software in Python — Part 2 Tutorial Tkinter

In Part 1, we built the logic to encrypt and decrypt messages, but imagine having a nice interface… Let me show you how

Maxime Godfroid
Aug 26, 2020 · 6 min read
Image for post
Image for post
Photo by Halacious on Unsplash

Introduction

In case you’ve missed Part 1, see the link below, but don’t forget to come back to Part 2:

Enters GUI

In this part we will focus on a simple interface, using a standard GUI Python library Tkinter. It will be built step by step to explain the logic, then rearranged at the end. Let’s get started.

Initial window

Code review line by line

LINE 1: We import Tkinter and will later refer to it as ‘tk’, a convention.

LINE 3: We initialize our Tkinter window, naming it root, also a commonly-used convention. The result looks like this.

Image for post
Image for post
Author’s screenshot

LINE 4: Setting up a title for our window.

LINE 5: Defining the minimum size for our window, this means the user will not be able to change the size below a width of 300 and a height of 230. That’s a size in pixels.

LINE 7: We create our Canvas. Canvas is a widget of Tkinter. This will contain our widgets, think buttons, entry field, from our interface. As arguments, we specify our root as Tk instance, and we set the window size. When generated, the window will open with that size.

LINE 8: Using .pack() on a widget will organize it as a block, and place it within the parent widget, the root.

LINE 10: By using .mainloop(), we initiate an infinite loop to run our window. It will remain active, listening (‘waiting’) for an event, processing it. The window will remain active as long as it’s not closed (or encounters an error).

The final result from those first lines:

Image for post
Image for post
Author’s screenshot

Adding Widgets

Code review line by line

This is an improvement from the code snippet above, hence explanations on lines from the previous snippets will be skipped.

LINE 10: We create the widget entry1, an entry field (‘input field’), and tie it to root.

LINE 11: We use .create_window() on our existing canvas1, set size parameters of the entry field and link it to entry1 via the window argument.

LINE 13–14: We create the widget label1, a field which will be holding/showing some text. As for entry1, we tie it to root and set dimensions.

LINE 16–17: We create the widget button1, a button which will submit our entry. The argument ‘text’ defines the text to show on the button. As for entry1 and label1, we tie it to root and set dimensions.

LINE 19: This will help us for the next few lines of code. We create v and assign it to a string variable, StringVar(). This will help us to track changes, widgets like RadioButton will require this to function properly.

LINE 20: We set up the v variable to ‘e’. Why? Read on!

LINE 22–24: We create a radio button, tie it to root, name it ‘Encrypt’, link it to the set it as value ‘e’, and link it to string variable v. As for previous widgets, we tie it to root, and set dimensions.

LINE 26: Same as for lines 22–24, except we name it ‘Decrypt’ and set ‘d’ as value.

So what? By using the v StringVar, we can define which radio button will be selected when the window is first generated. In our case, we have set v to be ‘e’ (line 20) and within our radio buttons, we defined the value of the encrypt button to be ‘e’. Those are thus tied, and ‘encrypt’ will appear checked upon generating the window.

Update on the look of our window:

Image for post
Image for post
Author’s screenshot

An improvement. But it’s only visual for now, we’ll have to link it with our encryption/decryption code to make it functional.

Bridging The Gap Between Front and Back-End

LINE 1: Define a function, which will get user inputs and treat them accordingly.

LINE 2: We get the value from the entry field entry1 and store it as x1.

LINE 3: Defining choice and assigning to the value we get from the v StringVar.

LINE 5:-6 If choice is equal to ‘e’, the code on line 6 is executed. When the user selects the radio button ‘encrypt’, the value of v will be ‘e’.

The ‘text’ attributed for label1 is set to the returned value from our code_message() function discussed in Part 1, which takes the user input x1 as an argument. As a reminder it looked like that:

LINE 8–9: The ‘else’ from our if statement, in this case, the user selected the ‘decrypt’ radio button. The function used is decode_message() which takes the user input x1 as an argument. As a reminder it looked like that:

LINE 20: This is where the magic happens. Within our button widget, we use the argument command and assign it to our function getResult. Any time the button will be clicked, the action will be to start the getResult. Using command now brings the widget to life, while previously it could be clicked but without any event taking place.

Tying It All Together

Code review line by line

LINE 1–43: You will have recognized what was discussed in Part 1.

LINE 46: We wrap the GUI code in a function called GUI, it makes it cleaner, easier to debug and read. In GUI we have a nested function, getResult, then the code to instantiate and build our Tkinter window.

LINE 83: We call the GUI function.

The final results:

Image for post
Image for post
Image for post
Image for post
Author’s screenshots

Well done, you did it !

Alternatives to Tkinter

  • PyQt,
  • Flexx,
  • Pyforms,

Conclusion

Mini-projects can give you confidence, and help you grow both your problem-solving and programming skills. It’s important to focus on projects that matter to you, let your mind wander a bit and you’ll soon find other ideas to bring to life. Start small, and build up 💪.

Happy Coding!

What Next? Part 3: Deploy as .exe!

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Maxime Godfroid

Written by

Tech & sports enthusiast | Avid runner | Also a data scientist. Follow me on Twitter @max_godfroid

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Maxime Godfroid

Written by

Tech & sports enthusiast | Avid runner | Also a data scientist. Follow me on Twitter @max_godfroid

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store