Home » Tutorials » How to Build an Internet Speed Test Tool with Python

How to Build an Internet Speed Test Tool with Python

In today’s tutorial, we are going to create a graphical user interface (GUI) where the user can measure the speed of the internet, specifically the Download speed, Upload speed, and Ping. With that being said, let’s get started!

Table of Contents

Necessary Libraries

First, make sure to install the tkinter and speedtest-cli libraries using the terminal or your command prompt for the code to function properly:

$ pip install tk
$ pip install speedtest-cli

Imports

from tkinter import *
from tkinter import messagebox
import speedtest

We start by importing the tkinter library, which allows us to create a graphical user interface (GUI), and from it we import the messagebox module that allows us to display messages.

Lastly, we import the speedtest module so we can measure the speed of the internet because this library performs speed tests.

Measure Speed Function

def measure_speed():
    st = speedtest.Speedtest()

Next, we create a function called measure_speed, which allows the users to choose the type of speed they want to test. In a way, it serves as the manager of the graphical user interface.

The second line is a tool that indicates how fast the selected option is.

Start Speed Test Function

   def start_speed_test():
       choice = choice_var.get()
 
 
 
       if choice == '1':
           download_speed = st.download() / (1024 * 1024) # Convert to Mbps
           result_label.config(text=f"Download Speed: {download_speed:.2f} Mbps")
       elif choice == '2':
            upload_speed = st.upload() / (1024 * 1024) # Convert to Mbps
            result_label.config(text=f"Upload Speed: {upload_speed:.2f} Mbps")
       elif choice == '3':
            server = st.get_best_server()
            ping = server["latency"]
            result_label.config(text=f"Ping: {ping} ms")
       else:
           messagebox.showerror("Error", "Invalid choice. Please select 1, 2, or 3.")

Following that, we define a function that takes the user’s choice from the radio button selection, tests its speed, updates the result, and displays an error message if the user does not select a choice.

Creating the Main Window

   root = Tk()
   root.title("Internet Speed Test - The Pycodes")
   root.geometry("500x200")

This part creates the main window, gives it a title, and defines its geometry.

Setting up Radio Buttons for User’s Choice

   choice_var = StringVar()
   choice_var.set('1')
 
 
 
   label = Label(root, text="Select the type of speed measurement :",font="arial 16 bold")
   label.pack()
 

 
   download_radio = Radiobutton(root, text="Download Speed", variable=choice_var, value='1',font="arial 12")
   download_radio.pack()
   upload_radio = Radiobutton(root, text="Upload Speed", variable=choice_var, value='2',font="arial 12")
   upload_radio.pack()
   ping_radio = Radiobutton(root, text="Ping", variable=choice_var, value='3',font="arial 12")
   ping_radio.pack()

After that, we set radio buttons (Download and Upload Speed, Ping), so that the user can choose the type of speed he wants to measure.

Adding a Button to Trigger Speed Test

    start_button = Button(root, text="Start Speed Test",font="arial 12", command=start_speed_test)
    start_button.pack()

Here, we created a button named “start speed test” Once clicked it calls the start_speed_test function.

Displaying Results Label

    result_label = Label(root, text="",font="arial 12")
    result_label.pack()

This code is for creating an empty label that displays the speed test results.

Running the Main Loop

    root.mainloop()

What this part does is keep the main window running and responsive to the user.

Main Block

if __name__ == "__main__":
measure_speed()

Lastly, this part makes sure that the measure_speed function is called only if this script is run directly and not imported as a module.

Example

Full Code

from tkinter import *
from tkinter import messagebox
import speedtest



def measure_speed():
   st = speedtest.Speedtest()



   def start_speed_test():
       choice = choice_var.get()



       if choice == '1':
           download_speed = st.download() / (1024 * 1024) # Convert to Mbps
           result_label.config(text=f"Download Speed: {download_speed:.2f} Mbps")
       elif choice == '2':
            upload_speed = st.upload() / (1024 * 1024) # Convert to Mbps
            result_label.config(text=f"Upload Speed: {upload_speed:.2f} Mbps")
       elif choice == '3':
            server = st.get_best_server()
            ping = server["latency"]
            result_label.config(text=f"Ping: {ping} ms")
       else:
           messagebox.showerror("Error", "Invalid choice. Please select 1, 2, or 3.")




   root = Tk()
   root.title("Internet Speed Test - The Pycodes")
   root.geometry("500x200")




   choice_var = StringVar()
   choice_var.set('1')




   label = Label(root, text="Select the type of speed measurement :",font="arial 16 bold")
   label.pack()




   download_radio = Radiobutton(root, text="Download Speed", variable=choice_var, value='1',font="arial 12")
   download_radio.pack()
   upload_radio = Radiobutton(root, text="Upload Speed", variable=choice_var, value='2',font="arial 12")
   upload_radio.pack()
   ping_radio = Radiobutton(root, text="Ping", variable=choice_var, value='3',font="arial 12")
   ping_radio.pack()




   start_button = Button(root, text="Start Speed Test",font="arial 12", command=start_speed_test)
   start_button.pack()



   result_label = Label(root, text="",font="arial 12")
   result_label.pack()



   root.mainloop()



if __name__ == "__main__":
   measure_speed()

Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top