Building Tools with Python

Animesh Javali
3 min readAug 2, 2020

Leveraging the open source…

Overview

A tool is a tiny piece of code that is placed third in the hierarchy, by size and power, after a large enterprise Software and a medium level Application. This is an insight to the vast capabilities of python, with its application-oriented modules and libraries, to create a simple tool.

Any tool should have at least one interface to talk to the user. It, in fact, can have multiple interfaces. One way is to have a CLI- Command Line Interface. A powerful tool, in some cases, will have a GUI.

In the following section, we will discuss which are the python modules that enable us to build a tool with CLI. In the later sections, we will learn to add a GUI to the tool.

Command Line Interface

There are two modules in python to accept command line arguments:

  1. sys.argv- This is a list which will contain the arguments that we pass through command line to the script. To use this, first we need to import the sys module. The first element sys.argv[0] will always be the script name. However, this module has limited functionality.
  2. argparse- This is a python standard library that is comprehensive to build a CLI tool. First we import the argparse module and then use the ArgumentParser method to create a root interface to which we go on adding features.
import argparseparser= argparse.ArgumentParser(description='This is a tool.')

There could be two types of arguments. Positional and Optional. Former is a mandatory argument that should be passed. Latter is only used if we need flags. For ex, verbose.

parser.add_argument('run', help='Run this tool.')
parser.add_argument("-V", help='Verbose.',required=False)

By setting required=False, we make verbose flag an optional argument. And finally, we parse the arguments using parse_args() method.

arg=parser.parse_args()'''
Add your logic.
'''

Adding GUI to the tool

Among so many modules to create GUI, prime ones are tkinter and pyglet. Pyglet is used to create advanced multimedia applications. It is capable of playing music, creating animation and much more. Tkinter is another module which is used to create simple UI for a tool. In this section, we will look through basics of tkinter.

from tkinter import *# Create a root window
window= Tk()
window.title('Beautiful Tool')
# Create a window of dimension 500x500. With 400 units on X-axis and 50 units on Y-axis.
window.geometry('500x500+400+50')

The above code creates a plain window. Now we can add some basic features like labels, buttons, entry to make it interactive. You can explore this module to add advanced widgets, images, gifs, and animation.

# Add Labels to display messages on UI.
label = Label(window, text = 'This is a beautiful Tool! Add your thoughts.')
label.pack()
# Add Entry to input string from users.
entry_line = Entry(window)
entry_line.pack()
# Add Buttons to your UI.
button = Button(window,text = 'Click', width=25)
button.pack()
'''
Add your logic.
'''
# Run this forever.
window.mainloop()

Conclusion

We understood the fundamentals of a simple tool and how python brings in infinite possibilities at our fingertips. We made our tool user interactive through CLI and GUI. You can now play around with these modules to create a powerful tool for your project.

--

--