Reader small image

You're reading from  Learn Python Programming, 3rd edition - Third Edition

Product typeBook
Published inOct 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781801815093
Edition3rd Edition
Languages
Right arrow
Authors (2):
Fabrizio Romano
Fabrizio Romano
author image
Fabrizio Romano

Fabrizio Romano was born in Italy in 1975. He holds a master's degree in Computer Science Engineering from the University of Padova. He's been working as a professional software developer since 1999. Fabrizio has been part of Sohonet's Product Team since 2016. In 2020, the Television Academy honored them with an Emmy Award in Engineering Development for advancing remote collaboration.
Read more about Fabrizio Romano

Heinrich Kruger
Heinrich Kruger
author image
Heinrich Kruger

Heinrich Kruger was born in South Africa in 1981. He holds a master's degree in Computer Science from Utrecht University in the Netherlands. He has been working as a professional software developer since 2014. Heinrich has been working alongside Fabrizio in the Product Team at Sohonet since 2017. In 2020, the Television Academy honored them with an Emmy Award in Engineering Development for advancing remote collaboration.
Read more about Heinrich Kruger

View More author details
Right arrow

GUIs and Scripting

"A user interface is like a joke. If you have to explain it, it's not that good."

– Martin LeBlanc

In this chapter, we're going to work on a project together. We are going to write a simple scraper that finds and saves images from a web page. We'll focus on three parts:

  • A simple HTTP web server in Python
  • A script that scrapes a given URL
  • A GUI application that scrapes a given URL

A graphical user interface (GUI) is a type of interface that allows the user to interact with an electronic device through graphical icons, buttons, and widgets, as opposed to text-based or command-line interfaces, which require commands or text to be typed on the keyboard. In a nutshell, any browser, any office suite such as LibreOffice, and, in general, anything that pops up when you click on an icon, is a GUI application.

So, if you haven't already done so, this would be the perfect time to...

First approach: scripting

Now, let's start writing the script. We'll go through the source in three steps: imports, argument parsing, and business logic.

The imports

Here's how the script starts:

# scrape.py
import argparse
import base64
import json
from pathlib import Path
from bs4 import BeautifulSoup
import requests

Going through the imports from the top, you can see that we'll need to parse the arguments, which we'll feed to the script itself (using argparse). We will need the base64 library to save the images within a JSON file (so we will also need json), and we'll need to open files for writing (using pathlib). Finally, we'll need BeautifulSoup for scraping the web page easily, and requests to fetch its content. We assume you're familiar with requests as we used it in Chapter 8, Files and Data Persistence.

We will explore the HTTP protocol and the requests mechanism in Chapter 14, Introduction to API Development...

Second approach: a GUI application

There are several libraries for writing GUI applications in Python. Some of the most famous ones are Tkinter, wxPython, Kivy, and PyQt. They all offer a wide range of tools and widgets that you can use to compose a GUI application.

The one we are going to use in this chapter is Tkinter. Tkinter stands for Tk interface and it is the standard Python interface to the Tk GUI toolkit. Both Tk and Tkinter are available on most Unix platforms, macOS X, as well as on Windows systems.

Let's make sure that tkinter is installed properly on your system by running this command:

$ python -m tkinter

It should open a dialog window, demonstrating a simple Tk interface. If you can see that, we're good to go. However, if it doesn't work, please search for tkinter in the Python official documentation (https://docs.python.org/3.9/library/tkinter.html). You will find several links to resources that will help you get up and running with...

Where do we go from here?

If you are interested in digging deeper into the world of GUIs, then we would like to offer the following suggestions.

The turtle module

The turtle module is an extended reimplementation of the eponymous module from the Python standard distribution up to version Python 2.5. It's a very popular way to introduce children to programming.

It's based on the idea of an imaginary turtle starting at the center of a Cartesian plane. You can programmatically command the turtle to move forward and backward, rotate, and so on; by combining all the possible moves, all sorts of intricate shapes and images can be drawn.

It's definitely worth checking out, if only to see something different.

wxPython, Kivy, and PyQt

After you have explored the vastness of the tkinter realm, we would suggest you explore other GUI libraries: wxPython (https://www.wxpython.org/), PyQt (https://www.riverbankcomputing.com/software/pyqt/), and Kivy (https...

Summary

In this chapter, we worked on a project together. We have written a script that scrapes a very simple web page and accepts optional commands that alter its behavior in doing so. We also coded a GUI application to do the same thing by clicking buttons instead of typing on a console. We hope you enjoyed reading it and following along as much as we enjoyed writing it.

We saw many different concepts, such as working with files and performing HTTP requests, and we talked about guidelines for usability and design.

We have only been able to scratch the surface, but hopefully you have a good starting point from which to expand your exploration.

Throughout the chapter, we have pointed out several different ways in which you could improve the application, and we have challenged you with a few exercises and questions. We hope you have taken the time to play with those ideas. You can learn a lot just by playing around with fun applications like the one we've coded together...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Python Programming, 3rd edition - Third Edition
Published in: Oct 2021Publisher: PacktISBN-13: 9781801815093
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Fabrizio Romano

Fabrizio Romano was born in Italy in 1975. He holds a master's degree in Computer Science Engineering from the University of Padova. He's been working as a professional software developer since 1999. Fabrizio has been part of Sohonet's Product Team since 2016. In 2020, the Television Academy honored them with an Emmy Award in Engineering Development for advancing remote collaboration.
Read more about Fabrizio Romano

author image
Heinrich Kruger

Heinrich Kruger was born in South Africa in 1981. He holds a master's degree in Computer Science from Utrecht University in the Netherlands. He has been working as a professional software developer since 2014. Heinrich has been working alongside Fabrizio in the Product Team at Sohonet since 2017. In 2020, the Television Academy honored them with an Emmy Award in Engineering Development for advancing remote collaboration.
Read more about Heinrich Kruger