Python - Courier Tracking Management System



Here, we will learn to design a courier tracking management system using the Python tkinter library and SQLite for the database with the features such as user registration, login and tracking of status of their consignment products through use of mobile numbers.

Features

This app consists the following features −

  • Repeat of registration and login options
  • It also targets tracking of consignments through mobile phone number.
  • They include informing customers about the product and the destination.

Required Libraries

To create courier tracking management system, you will have to install the following libraries −

  • tkinter − It is a built-in GUI interface for Python basically used for creating windows-based applications. It is used to develop the user interface so that the users are able to interact with the system.
  • SQLite3 − SQLite is a C library and it is implemented as x86 machine code and it is a light weight and disk-based database. It is perfect for small scale applications where one doesn't need full bed of services of a database server. To work with SQLite, we don't need to install SQLite as it comes along with Python.
  • random − The random module is to get random consignment number for products. This module is a part of Python and you do not need to install it.

App Workflow and Functional Components

1. Database Setup

First create an SQLite database and define a table user to store the username, password, and mobile number of users. The CREATE TABLE IF NOT EXISTS query ensures that the table is only created if it doesn't already exist.

2. Class Structure

The main function of the program is in the main class. Here, we see all variables and methods that control the GUI behavior and database interactions.

3. Login

The login() method checks the credentials entered by the user against the records in the SQLite database. If the credentials match, the user is authenticated and allowed to track products.

4. New User Registration

In the new_user() method, we allow new users to create accounts by entering a username, password, and mobile number. The system checks if the username already exists before adding a new record to the database.

5. Tracking a Courier

Once logged in, the user can track their courier by entering the mobile number and selecting the product. The courier method retrieves the courier details from the database, displays them in the UI, and generates a random product ID.

6. GUI Widgets

The widgets method defines all the GUI components, including labels, entry fields, buttons, and comboboxes. It creates the main layout for the login, registration, and consignment tracking forms.

Python Code for Courier Tracking Management System

The below is the complete code to design this application −

from tkinter import *
from tkinter import messagebox as ms
from tkinter import ttk
import sqlite3
import random

# Database
with sqlite3.connect('Akash5.db') as db:
   c = db.cursor()
try:
   c.execute('CREATE TABLE IF NOT EXISTS user (username TEXT NOT NULL, password TEXT NOT NULL, mobile TEXT NOT NULL);')
except:
   pass
db.commit()
db.close()

class main:
   def __init__(self, master):
      self.master = master

      self.username = StringVar()
      self.password = StringVar()
      self.n_username = StringVar()
      self.n_password = StringVar()
      self.n_reg = StringVar()
      self.n_mobile = StringVar()
      self.mobile11 = StringVar()
      self.destination = StringVar()
      self.selected_product = StringVar()
      self.widgets()

   def login(self):
      with sqlite3.connect('Akash5.db') as db:
         c = db.cursor()

         find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
         c.execute(find_user, [(self.username.get()), (self.password.get())])
         result = c.fetchall()

      if result:
         self.track()
      else:
         ms.showerror('Oops!', 'Username Not Found.')

   def new_user(self):
      with sqlite3.connect('Akash5.db') as db:
         c = db.cursor()
      if self.n_username.get() != ' ' and self.n_password.get() != ' ' and self.n_mobile.get() != ' ':
         find_user = ('SELECT * FROM user WHERE username = ?')
         c.execute(find_user, [(self.n_username.get())])

      if c.fetchall():
         ms.showerror('Error!', 'Username Taken. Try a Different One.')
      else:
         insert = 'INSERT INTO user(username, password, mobile) VALUES(?, ?, ?)'
         c.execute(insert, [(self.n_username.get()), (self.n_password.get()), (self.n_mobile.get())])
         db.commit()

         ms.showinfo('Success!', 'Account Created!')
         self.log()
      else:
         ms.showerror('Error!', 'Please Enter the details.')

   def consignment(self):
      try:
         with sqlite3.connect('Akash5.db') as db:
            c = db.cursor()

         find_user = ('SELECT * FROM user WHERE mobile= ?')
         c.execute(find_user, [(self.mobile11.get())])
         result = c.fetchall()

         if result:
            self.track()
            self.crff.pack_forget()
            self.head['text'] = self.username.get() + '\n Your Product Details'

            # Get the latest value of destination before packing the frame
            destination_value = self.destination.get()

            # Update product details in consi frame
            self.consi.pack()
            for widget in self.consi.winfo_children():
               widget.destroy()  # Clear previous widgets in consi frame

               Label(self.consi, text='Product ID:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
               Label(self.consi, text=random.randint(565154, 99994216), font=('Arial', 13), pady=5, padx=5, fg="white", bg="black").grid(row=0, column=1)

               Label(self.consi, text='Product Name:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
               Label(self.consi, text=self.selected_product.get(), font=('Arial', 13), pady=5, padx=5, fg="white", bg="black").grid(row=1, column=1)

               Label(self.consi, text='Destination:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
               Label(self.consi, text=destination_value, font=('Arial', 13), pady=5, padx=5, fg="white", bg="black").grid(row=2, column=1)

               Label(self.consi, text='Product Status: Pending', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(row=3, column=1)

               Button(self.consi, text='Back', bd=2, font=('Arial', 13), padx=6, pady=6, command=self.track1, bg="gray").grid(row=4, column=0)
         else:
            ms.showerror('Oops!', 'Mobile Number Not Found.')
      except:
         ms.showerror('Oops!', 'Mobile Number Not Found.')

   def track1(self):
      self.consi.pack_forget()
      self.head['text'] = self.username.get() + '\n Track your Product'
      self.crff.pack()

   def log(self):
      self.username.set('')
      self.password.set('')
      self.crf.pack_forget()
      self.head['text'] = 'Login'
      self.logf.pack()

   def cr(self):
      self.n_username.set('')
      self.n_password.set('')
      self.logf.pack_forget()
      self.head['text'] = 'Create Account'
      self.crf.pack()

   def track(self):
      self.logf.pack_forget()
      self.head['text'] = self.username.get() + '\n Track your Product'
      self.crff.pack()

   def widgets(self):
      self.head = Label(self.master, text='LOGIN', font=('Arial', 20), pady=10, fg="white", bg="black")
      self.head.pack()

      self.logf = Frame(self.master, padx=10, pady=10, bg="black")

      Label(self.logf, text='Username:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Entry(self.logf, textvariable=self.username, bd=3, font=('Arial', 15)).grid(row=0, column=1)
      Label(self.logf, text='Password:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Entry(self.logf, textvariable=self.password, bd=3, font=('Arial', 15), show='*').grid(row=1, column=1)
      Button(self.logf, text=' Login ', bd=2, font=('Arial', 13), padx=6, pady=6, command=self.login, bg="gray").grid(row=8, column=0)
      Button(self.logf, text=' New user ', bd=2, font=('Arial', 13), padx=6, pady=6, command=self.cr, bg="gray").grid(row=8, column=1)

      self.logf.pack()

      self.crf = Frame(self.master, padx=10, pady=10, bg="black")
      Label(self.crf, text='Username:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Entry(self.crf, textvariable=self.n_username, bd=3, font=('Arial', 15)).grid(row=0, column=1)

      Label(self.crf, text='Password:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Entry(self.crf, textvariable=self.n_password, bd=3, font=('Arial', 15), show='*').grid(row=1, column=1)

      Label(self.crf, text='Mobile No.:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Entry(self.crf, textvariable=self.n_mobile, bd=3, font=('Arial', 15)).grid(row=5, column=1)

      Button(self.crf, text='Create Account', bd=2, font=('Arial', 13), padx=6, pady=6, command=self.new_user, bg="gray").grid(row=11, column=0)
      Button(self.crf, text='Go to Login', bd=2, font=('Arial', 13), padx=6, pady=6, command=self.log, bg="gray").grid(row=11, column=1)

      self.crff = Frame(self.master, padx=10, pady=10, bg="black")
      Label(self.crff, text='Consignment No:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Entry(self.crff, bd=3, font=('Arial', 15)).grid(row=0, column=1)
      Label(self.crff, text='Mobile no:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Entry(self.crff, bd=3, textvariable=self.mobile11, font=('Arial', 15)).grid(row=1, column=1)

      Label(self.crff, text="Select Product:", font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      self.selected_product.set("Bag")
      products = ["Bag", "Colgate", "Shoe", "Redmi 2", "Jeans", "Mac", "Ipad", "Pen", "Book", "Shirt"]
      product_menu = ttk.Combobox(self.crff, textvariable=self.selected_product, values=products, font=('Arial', 13))
      product_menu.grid(row=2, column=1)

      Label(self.crff, text="Destination:", font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Entry(self.crff, textvariable=self.destination, bd=3, font=('Arial', 15)).grid(row=3, column=1)

      Button(self.crff, text='Track', bd=2, font=('Arial', 13), padx=6, pady=6, command=self.consignment, bg="gray").grid(row=4, column=0)

      self.consi = Frame(self.master, padx=10, pady=10, bg="black")
      Label(self.consi, text='Product ID:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Label(self.consi, text=random.randint(565154, 99994216), font=('Arial', 13), pady=5, padx=5, fg="white", bg="black").grid(row=0, column=1)

      Label(self.consi, text='Product Name:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Label(self.consi, text=self.selected_product.get(), font=('Arial', 13), pady=5, padx=5, fg="white", bg="black").grid(row=1, column=1)

      Label(self.consi, text='Destination:', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(sticky=W)
      Label(self.consi, text=self.destination.get(), font=('Arial', 13), pady=5, padx=5, fg="white", bg="black").grid(row=2, column=1)

      Label(self.consi, text='Product Status: Pending', font=('Arial', 15), pady=5, padx=5, fg="white", bg="black").grid(row=3, column=1)

      Button(self.consi, text='Back', bd=2, font=('Arial', 13), padx=6, pady=6, command=self.track1, bg="gray").grid(row=4, column=0)

if __name__ == '__main__':
   root = Tk()
   root.title('Track Consignment')
   root.geometry('800x450+300+300')
   root.configure(bg='black')  # Set background to black
   main(root)
   root.mainloop()

App Explanation with Output Screenshots

1. Login Screen

This is the first screen the user sees when they launch the app. The login screen consists of: Username and password input fields, buttons for login and new user registration −

Courier Tracking Management System 1

2. New User Registration Screen

When the user clicks the "New User" button, they are taken to the registration screen. We created new username "Rama DAS" and add Password, phone number. This form asks the user for −

Courier Tracking Management System 2

3. Courier Tracking Screen

Once a user successfully logs in, they are taken to the consignment tracking screen. They can enter a mobile number to find the consignment linked to it. Choose a product and view its details like destination and status −

Courier Tracking Management System 3

4. Courier Details Output

After entering the mobile number and product, the system displays courier details such as: Product ID (randomly generated), Product Name, Destination, Status (Pending by default)

Courier Tracking Management System 4

Code Explanation

  • Import Libraries − The script starts off with importing a few modules primarily useful in GUI development from Tkinter, for database operations from sqlite3, and for getting random numbers from random.
  • Database Setup − It creates a new SQLite database known as 'Akash5.db' and it also sets out user table in case it does not exist to be used in storing users’ details, such as credentials and/or mobile numbers.
  • Class Definition − There is the main class, which contains all the application logic, database connection and the GUI management.
  • Initialization − In the __init__ method, the class creates StringVar variables used in the storage of user's inputs and product details.
  • Login Method − It means that the login method checks user inputs with data stored in a database. It will make track() call if found, and if not, then display an error message.
  • New User Method − The method called new_user is responsible for the creation of account. It first verifies whether the given username is available in the database and then creates a new entry for the new user.
  • Consignment Method − In this method, the users can track a consignment by entering their mobile number as a code. In case, the mobile number is traced, it exhibits consignment information.
  • Track1 Method − That way, the consignment details frame is minimized while the product tracking interface appears on the screen.
  • Log Method − The log method erases the text fields of login and changes to the login interface.
  • Cr Method − The cr method erases registration fields and goes to the account creation screen.
  • Track Method − This method exchanges the GUI to the consignment tracking frame.
  • Widgets Method − This method brings out the whole GUI that consists of login, registration, consignment tracking, and product detail frames.
  • Login Frame − It contains the login frame (logf) with boxes for entering the username and password as well as buttons "Log In" and "Create An Account".
  • Registration Frame − The registration frame (crf) consists username, password, and mobile number with the options Create a new account and go to login.
  • Consignment Frame − In the consignment frame (crff), there are some fields such as consignment number, mobile number to identify customers and product selection, and destination consignment buttons to track the consignment.
  • Consignment Details Frame − The consignment details frame known as consi shows information on the consignment containing the ID, name, and destination of the product.
  • Random Product ID − The randint() method selects a random product ID for the consignment details.
  • Product Selection − As for the field that lets the users choose a product, the technique here, is a drop-down list (with the help of ttk.combobox).
  • Error Handling − The simplest form of exception handling is used where the message box is used to display error messages for invalid input or mobile number not found.
  • Main Execution − The script is the first TI-Kinter script that has created a GUI window by providing it with a title and size, and start the application by creating an instance of the main class.
python_reference.htm
Advertisements