Python Internet Speed Test Using Tkinter

Faraz

By Faraz - June 14, 2024

Learn how to create a Python internet speed test with a GUI using Tkinter. Step-by-step guide to coding and running your speed test application.


python-internet-speed-test-using-tkinter.webp

Table of Contents

  1. Introduction
  2. Understanding Internet Speed
  3. Setting Up Your Environment
  4. Full Internet Speed Test Source Code
  5. Explanation of Internet Speed Test Source Code
  6. Conclusion
  7. FAQs

1. Introduction

Creating an internet speed checker is a great way to learn Python programming and Tkinter, a standard GUI toolkit. This project will guide you through building a functional application that measures your internet speed and displays the results in a user-friendly interface. By the end of this guide, you will have a working internet speed checker and a better understanding of how to create GUI applications with Python.

2. Understanding Internet Speed

What is Internet Speed?

Internet speed refers to the rate at which data is transferred between your device and the Internet. It's typically measured in megabits per second (Mbps). There are two main components of internet speed:

  • Download Speed: How fast data is downloaded from the internet to your device.
  • Upload Speed: How fast data is uploaded from your device to the internet.

Factors Affecting Internet Speed

Several factors can influence your internet speed:

  • Bandwidth: The maximum amount of data that can be transmitted over an internet connection in a given time.
  • Latency: The delay before a transfer of data begins following an instruction.
  • Network Congestion: High traffic on the network can slow down internet speeds.
  • Hardware and Software: The performance of your modem, router, and devices can impact speed.

3. Setting Up Your Environment

Before starting, ensure that you have Python installed on your computer. You can download the latest version of Python from the official website. Additionally, you will need to install Tkinter, which is usually included with Python installations.

Installing Tkinter

Tkinter comes pre-installed with Python. You can check if it's installed by running:

import tkinter
print(tkinter.TkVersion)

If it's not installed, you can install it using:

pip install tk

Installing Necessary Libraries

For this project, we will use the speedtest-cli library to measure internet speed. You can install it using pip, the Python package manager. Open your terminal and run the following command:

pip install speedtest-cli

This library provides a simple way to check your internet speed programmatically. Additionally, ensure you have Tkinter installed for creating the GUI. With these tools ready, you can start building your application.

4. Full Internet Speed Test Source Code

from tkinter import *
from tkinter.ttk import Progressbar
import speedtest
import time


def animate_speed(speed_value, progress_bar, scaling_factor):
    max_value = speed_value * scaling_factor
    increment = max_value / 100
    for i in range(int(max_value) + 1):
        if i > 0.9 * max_value:
            break
        progress_bar['value'] = i
        progress_bar.update()
        time.sleep(0.02)


def check_speed():
    st = speedtest.Speedtest()
    download_speed = st.download() / 1000000
    upload_speed = st.upload() / 1000000
    ping = st.results.ping
    download_label.config(text=f"Download Speed: {download_speed:.2f} Mbps")
    upload_label.config(text=f"Upload Speed: {upload_speed:.2f} Mbps")
    ping_label.config(text=f"Ping: {ping:.2f} ms")
    animate_speed(download_speed, download_progress, 5)
    animate_speed(upload_speed, upload_progress, 3)
    animate_speed(ping, ping_progress, 2)


root = Tk()
root.title("Internet Speed Checker")
root.config(bg="#212121")
root.geometry("500x400")
root.resizable(False, False)

label1 = Label(root, text="Internet Speed Checker", font=(
    "Helvetica", 30, "bold"), bg="#212121", fg="#ffffff")
label1.pack()

download_label = Label(root, font=("Helvetica", 16),
                       bg="#212121", fg="#ffffff")
download_label.pack(pady=10)

download_progress = Progressbar(
    root, orient=HORIZONTAL, length=300, mode='determinate')
download_progress.pack(pady=10)

upload_label = Label(root, font=("Helvetica", 16), bg="#212121", fg="#ffffff")
upload_label.pack(pady=10)

upload_progress = Progressbar(
    root, orient=HORIZONTAL, length=300, mode='determinate')
upload_progress.pack(pady=10)

ping_label = Label(root, font=("Helvetica", 16), bg="#212121", fg="#ffffff")
ping_label.pack(pady=10)

ping_progress = Progressbar(root, orient=HORIZONTAL,
                            length=300, mode='determinate')
ping_progress.pack(pady=10)

check_speed()

button_refresh = Button(root, text="Refresh", font=(
    "Helvetica", 14, "bold"), bg="#03a9f4", fg="#ffffff", command=check_speed)
button_refresh.pack(pady=20)

root.mainloop()

5. Explanation of Internet Speed Test Source Code

Here's a detailed explanation of the code:

Imports

from tkinter import *
from tkinter.ttk import Progressbar
import speedtest
import time
  • tkinter: Standard Python interface to the Tk GUI toolkit.
  • ttk: Provides themed widgets for Tk.
  • speedtest: A module to test the internet bandwidth using Speedtest.net.
  • time: Standard Python module for time-related functions.

Functions

animate_speed
def animate_speed(speed_value, progress_bar, scaling_factor):
    max_value = speed_value * scaling_factor
    increment = max_value / 100
    for i in range(int(max_value) + 1):
        if i > 0.9 * max_value:
            break
        progress_bar['value'] = i
        progress_bar.update()
        time.sleep(0.02)
  • animate_speed takes a speed_value, a progress_bar, and a scaling_factor.
  • It calculates max_value by multiplying the speed_value by scaling_factor.
  • It increments the progress bar in small steps (1% of the max value).
  • The animation stops once it reaches 90% of the max value.
  • progress_bar['value'] = i updates the progress bar.
  • time.sleep(0.02) creates a delay to visualize the progress bar filling up.
check_speed
def check_speed():
    st = speedtest.Speedtest()
    download_speed = st.download() / 1000000
    upload_speed = st.upload() / 1000000
    ping = st.results.ping
    download_label.config(text=f"Download Speed: {download_speed:.2f} Mbps")
    upload_label.config(text=f"Upload Speed: {upload_speed:.2f} Mbps")
    ping_label.config(text=f"Ping: {ping:.2f} ms")
    animate_speed(download_speed, download_progress, 5)
    animate_speed(upload_speed, upload_progress, 3)
    animate_speed(ping, ping_progress, 2)
  • check_speed uses speedtest.Speedtest to measure download speed, upload speed, and ping.
  • It updates the labels with the measured speeds.
  • It calls animate_speed to animate the progress bars for download speed, upload speed, and ping.

GUI Setup

root = Tk()
root.title("Internet Speed Checker")
root.config(bg="#212121")
root.geometry("500x400")
root.resizable(False, False)
  • Initializes the main application window with Tk().
  • Sets the title, background color, window size, and makes the window non-resizable.

Labels and Progress Bars

label1 = Label(root, text="Internet Speed Checker", font=("Helvetica", 30, "bold"), bg="#212121", fg="#ffffff")
label1.pack()

download_label = Label(root, font=("Helvetica", 16), bg="#212121", fg="#ffffff")
download_label.pack(pady=10)

download_progress = Progressbar(root, orient=HORIZONTAL, length=300, mode='determinate')
download_progress.pack(pady=10)

upload_label = Label(root, font=("Helvetica", 16), bg="#212121", fg="#ffffff")
upload_label.pack(pady=10)

upload_progress = Progressbar(root, orient=HORIZONTAL, length=300, mode='determinate')
upload_progress.pack(pady=10)

ping_label = Label(root, font=("Helvetica", 16), bg="#212121", fg="#ffffff")
ping_label.pack(pady=10)

ping_progress = Progressbar(root, orient=HORIZONTAL, length=300, mode='determinate')
ping_progress.pack(pady=10)
  • Creates and configures labels and progress bars for displaying and animating the download speed, upload speed, and ping.
  • Uses pack() to arrange the widgets in the window.

Check Speed and Refresh Button

check_speed()

button_refresh = Button(root, text="Refresh", font=("Helvetica", 14, "bold"), bg="#03a9f4", fg="#ffffff", command=check_speed)
button_refresh.pack(pady=20)
  • Calls check_speed to measure and display the internet speed when the application starts.
  • Creates a "Refresh" button that calls check_speed when clicked.

Main Loop

root.mainloop()
  • Starts the Tkinter event loop, which waits for user interactions and updates the GUI accordingly.

6. Conclusion

Creating an internet speed test application using Python and Tkinter is a fun and educational project. It helps you understand both GUI development and network programming. With a few lines of code, you can build a useful tool to monitor your internet speed.

7. FAQs

Q1: Is Tkinter included with Python?

A1: Yes, Tkinter comes pre-installed with Python, so you don't need to install it separately.

Q2: I’m having trouble installing speedtest-cli. What should I do?

A2: Ensure you have pip installed. Use pip install speedtest-cli and check your internet connection.

Q3: How can I add more features to my application?

A3: You can add features like ping tests, server selection, and historical data logging to enhance your application.

Q4: My application runs slow. How can I improve its performance?

A4: Optimize the code by minimizing GUI updates and handling data efficiently. Avoid running intensive tasks on the main thread.

Q5: How can I share my application with others?

A5: Use packaging tools like PyInstaller to create an executable that can be shared without requiring users to have Python installed.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺