Building Apps in Python

Learn about Python, building apps and how to automate various tasks in Python!

Hey there everyone! This series of our newsletter brings you the exciting world of app building using Python, and Python Automation!
Automate any task that you like, with a few short lines of code and save hours of your time!

Introduction

Python is a well known and supported programming language that can be used to build various apps for almost any platform, either Windows, Mac, Linux, Android or iOS!

The current version of Python in use is Python 3, with the latest release being Python 3.11.

In this edition, you will learn how to set up and install Python, and learn to build Graphical User Interfaces with Tkinter!

1. Setting up Python

To download Python, click the link below. Alternatively you can download it from Microsoft Store as well, if you are using Windows.

Click on the Download Python button to download the offline setup. If using any other OS than Windows, click on the link to your OS, written in yellow.

Run the setup that has been downloaded, and Python will be installed on your device.

2. Setting up a text editor or an IDE

Now that we have installed Python successfully, we need to install either a text editor or an Integrated Development Environment (IDE). WE have multiple choices for that, but I prefer these two:

  • PyCharm

  • VS Code

I personally use PyCharm so we are going to be setting PyCharm up, by downloading the setup installer from the website below.

If you want to download VS Code, click the link below.

Go to the website, and scroll down to find the community edition, and download the installer. Once the installer is downloaded, run it by double clicking, and proceed with the installation.

Congratulations! You have successfully installed Python and related software to run Python scripts!

Let’s get to coding our first automation task!

3. Coding, Libraries

For those who want a refresher, or are new to python, we have compiled a small list of YouTube videos you can watch to learn the basics of Python as fast as possible, and familiarise yourself with the ABCs of object oriented programming as well!

  1. Python as Fast as Possible

3. Python in 1 minute

I would personally recommend Video #2 for complete beginners, as it is very comprehensive and covers every aspect of the python language that you need to know.

Before we start writing code, there are a few things that you need to be familiar with. Python is a vast programming language with thousands of libraries to help you in every task possible. But you might be wondering what is a Python Library?

A Python library is a collection of pre-written Python code that provides useful functionality to developers.”

Some key things to know about Python libraries:

  • They are packages or modules of Python code that can be imported and used in a Python project. Popular examples include NumPy, Pandas, TensorFlow, PyTorch, SciPy, etc.

  • Libraries contain classes, functions, and other objects that have already been written, tested, and organized to help developers solve common problems and tasks more efficiently.

  • Using libraries allows developers to build on existing code rather than writing everything from scratch. This saves time and reduces code duplication.

  • Libraries are typically designed to be reusable and modular. Developers can import and use just the parts they need for their application.

  • Many libraries are open source and community developed. Developers can view code, contribute fixes/enhancements, report issues, and request features.

  • Popular libraries provide documentation and examples to help developers learn how to use them effectively.

  • Libraries must be installed before they can be imported and used in Python code. Tools like pip, conda, etc are used to manage library installations.

  • The Python Package Index (PyPI) is a central repository that hosts thousands of open source Python libraries for convenient download and installation.

To install any python library, you just need to run a few commands via the terminal or command prompt.

pip install (library name)

This command works for all OS.

4. Tkinter Tutorial

Introduction:
  • Tkinter is a GUI toolkit that comes included with Python. So you don't need to install anything extra to use it. It provides a simple way to build graphical interfaces in Python.

Importing Tkinter:
  • To use Tkinter, you first need to import it. This is done by adding the following import statement at the top of your Python file:

import tkinter as tk
Creating the main window:
  • To create the main window, instantiate a Tk object:

root = tk.Tk()

There are a lot of elements, which are called widgets, that you can add to the GUI of your app with tkinter. A few of them are listed below:

  • Label - Displays text

  • Button - A clickable button

  • Entry - A single line text input field

  • Text - A multi-line text input field

  • Checkbutton - A checkbox

  • Radiobutton - A radio button for selection

  • Frame - An invisible container to group widgets

  • Canvas - Area for drawing shapes and plotting graphics

  • Menu - Dropdown menu bar

  • Message - Displays a multiline message

  • Scale - Slider or dial to select a value

  • Listbox - A list of selectable text items

  • Combobox - Dropdown list to select an option

  • Spinbox - Text field with up/down arrows

  • Scrollbar - Add scrolling capability

  • ProgressBar - Visual progress bar

  • Toplevel - Secondary window

  • MessageBox - Pop-up modal dialog box

Adding widgets:
  • To add widgets like buttons, labels, and input boxes, you first need to create them by instantiating the appropriate class. For example:

label = tk.Label(root, text="Hello World") 
button = tk.Button(root, text="Click Me")
entry = tk.Entry(root)
  • Then you can use the widget's .pack() method to add it to the main window:

label.pack()
button.pack()
entry.pack()
Launching the GUI:
  • To launch and run the GUI, you need to call the main window's .mainloop() method:

root.mainloop()
So in summary, the basic steps are:
  1. Import tkinter

  2. Create the main window

  3. Add widgets

  4. Call mainloop() to launch the GUI

Play around with the knowledge that you have gained through this tutorial, and let me know about what you build with it!!

Stay tuned for more tutorials like this tomorrow!

Typharius

The Aivelution