This tutorial will walk you through the process of creating an ebook reader in Python. You’ll start by defining a few constants and structures, then move on to write the code that will make the application work.
Define Constants and Structures
Constants and structures are like two sides of the same coin. Every program needs them, but you should learn to use them in the right way. Constants define important values or parameters for your application, while structures define the general layout and design of your application.
Constants are usually defined with the const
statement at the top of your source code files. Structures are usually defined with the struct
statement at the top of your source code files. For example:
const TITLE = "My Awesome Ebook Reader"
const DESCRIPTION = "This is a simple program that will "
const PROGRAM = "demonstrate how to use Python to "
const BUILD_INFO = """export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$LIBRARY_PATH
export PYTHONPATH=${PYTHONPATH}:$(find -L -name libpython3.6m.dylib | head -1)
source /opt/local/bin/python3.6m
"""
These three lines of code define three important constants for our ebook reader application – TITLE
, DESCRIPTION
, and PROGRAM
. We can use these constants across our code to provide meaningful names whenever we use them. We’ll discuss more about constants and why they are important in the next section.
Create Lists To Hold Our Data
Data structures are very important in every area of programming. In this section we’ll define a few data structures that will hold data for our ebook reader application.
A list is a linear collection of data elements. It is a fundamental data structure in Python, and it can hold any type of data, which may be strings or integers or even other lists. We’ll use an empty list as a placeholder for our data, and then we’ll populate it with important information as we write our program:
import sys
sys.path.append("./resources")
from PySide2.QtCore import (QDebug, QList, QObject, QString, QWidget)
from PySide2.QtGui import (QDesktopWidget, QFont, QPushButton, QImage, QSize)
from PySide2 import QtCore, QtGui
Creating lists is easy – just use the list()
function, and you’re ready to populate it with elements.
Example 1: Define a list to contain the names of our five favorite authors.
authors_list = ["Tom Clancy", "Dan Brown", "David Baldacci", "Chuck Palahniuk", "Stephen King"]
(Notice how we used a list comprehension here – it’s one of Python’s many magical features that makes programming so much easier.)
We can use this list to populate an <input>
tag on our website with the names of our favorite authors, as follows:
<input type="text" name="author" value="{{.}}"/>
Here we see that value="{{.}}" - the {{.}} will be replaced with each author's name when the form is submitted.
Now we need to add a few more lines of code to properly define our authors_list
variable. Above the author
tag on our website, add the following code:
<input type="submit" name="submit" value="Submit" />
This will submit the form with the value of the author
tag, which will be the value of the authors_list
variable - in this case, the five names of our favorite authors.
Now that we've defined our lists, it's time to move on to the code that will make our application work.
Determine What The User's Goal Is
What will the user want to do with this application? Before we begin coding, it is important to determine the user's goal as precisely as possible. There are several ways to do this. One of the simplest and most effective methods is through the use of tasks and user journeys. A task is a small, self-contained part of a workflow that a user must accomplish in order to achieve their goal.
A user journey is a series of tasks that a user must follow in order to accomplish a desired goal. A journey can be as simple or as complex as you need it to be, and it should include the smallest logical chunk of tasks required to complete it. The more granular you can make your user journeys, the more effective they will be in helping you determine the right goal for your application.
Define The Interface
The frontend of our application will be made up of several user-facing widgets, such as combo boxes, text fields, and buttons. These widgets will allow the user to interact with the application and determine what happens next.
The back-end of our application will be made up of logic that interprets user input, determines which task to perform, and updates our data structures, all without our intervention.
Start Small
An important principle in software development is to start small and simple. It is a good idea to start with a few key interactions that will allow the user to successfully accomplish their goal. Once you have these working, you can add more complex behavior on top of them.
Create A Task For Each Step
We've discussed how important it is to define our user's goal precisely. Without clear instructions, without a defined end point, it is difficult to create an interactive application. To further improve the effectiveness of our tutorial, we will define each step our user will need to follow in order to complete their goal. For example, let's say our goal is to add an item to the shopping cart. We'll add a single step to our task, namely Step 0: Add Item To Cart.
The first thing the user will want to do is add an item to the cart. In terms of our application, this means adding a book to our virtual bookshelf. Let's say the user selects the Add
option on a combo box, which represents adding an item to the cart. As soon as the user clicks the Add
button, a new task will be started – Step 0: Add Item To Cart.
Tasks should be short and sweet. If a task spans multiple screens, it's a good idea to break it down into smaller pieces. This will improve the user's experience by reducing the amount of time it takes to complete the task, as well as minimize the amount of scrolling that is required.
Use Journeys To Flow Through The Application
A journey is a series of tasks that a user must follow in order to accomplish a desired goal. The tasks in a journey should be connected to each other in some way – it is often helpful to think of a journey as a path through a task tree. This path defines the order in which the user will need to perform the tasks in order to reach their goal.