For those of us who’ve fallen in love with Python, diving into the heart of our computers is just another reason to adore it. We’ve already seen how to get system information using Python. So, in today’s tutorial, we’re going to focus on getting hardware information using wmi
library in Python.
PS: it is important to note that this code only works in Windows, If you’re using Linux or macOS, we’ve got you covered as well. Please check out this article for monitoring system and GPU information in Python tailored to your operating system.
Let’s get started!
Table of Contents
- Necessary Libraries
- Imports
- get_hardware_info Function
- Creating the Main Window
- Creating the Scrolled Text Widget
- Creating the Fetch Button
- Running the Main Loop
- Example
- Full Code
Necessary Libraries
Make sure to install the tkinter and WMI libraries via the terminal or your command prompt for the code to function properly:
$ pip install tk
$ pip install wmi
Imports
import tkinter as tk
from tkinter import scrolledtext
import wmi
Well, first of all, I’m sure any of us using an app or a program would want it to be easy to use, which is why we import tkinter
as it allows us to create an easy-to-use graphical user interface (GUI).
Since we’re going to retrieve hardware information from our system, we need to be able to scroll through this information; hence, we import the scrolledtext
module.
For a better understanding of our system, we will need precise information. For this purpose, we will use the wmi
library, which allows access to Windows Management Instrumentation for retrieving system information and performing management tasks.
get_hardware_info Function
Next, we define the function that will get the hardware information of our system, and here we’ll look at how it works:
Defining the Function
This part ensures that the hardware_info
widget is empty by deleting any text from it, then it establishes a connection to our Windows Management Instrumentation (WMI) interface.
def get_hardware_info():
hardware_info.delete(1.0, tk.END) # Clear previous info
c = wmi.WMI()
Retrieving Disk Drive Information
Using the wmi
library, this code will get each disk drive information from our system, such as “the name of the disk drive” and “the type of the interface” used by it, as well as “the size” in gigabytes, and also “the serial number” assigned to it and “whether it is operational or not“. All of this information will be retrieved by going through the disk drives one by one and then inserting it into the hardware_info
widget to be displayed.
hardware_info.insert(tk.END, "Disk Drives:\n")
for disk in c.Win32_DiskDrive():
hardware_info.insert(tk.END, f"Model: {disk.Model}\n")
hardware_info.insert(tk.END, f"Interface Type: {disk.InterfaceType}\n")
hardware_info.insert(tk.END, f"Size: {int(disk.Size) / (1024 ** 3):.2f} GB\n")
hardware_info.insert(tk.END, f"Serial Number: {disk.SerialNumber}\n")
hardware_info.insert(tk.END, f"Status: {disk.Status}\n\n")
Retrieving Network Adapter Information
This one goes through each network adapter in our system and retrieves information about them such as “the manufacturer“, “network connection ID“, and “the speed“, then it will insert the acquired information in the hardware_info
widget with a heading that says Network Adapters.
hardware_info.insert(tk.END, "\nNetwork Adapters:\n")
for interface in c.Win32_NetworkAdapter():
hardware_info.insert(tk.END, f"Name: {interface.Name}\n")
hardware_info.insert(tk.END, f"Description: {interface.Description}\n")
hardware_info.insert(tk.END, f"MAC Address: {interface.MACAddress}\n")
if interface.Speed is not None:
if interface.Speed.isdigit(): # Check if speed is a digit
hardware_info.insert(tk.END,
f"Speed: {int(interface.Speed) / 10 ** 6} Mbps\n") # Convert to integer before division
else:
hardware_info.insert(tk.END, f"Speed: Unknown\n")
else:
hardware_info.insert(tk.END, f"Speed: Unknown\n")
hardware_info.insert(tk.END, f"Manufacturer: {interface.Manufacturer}\n")
hardware_info.insert(tk.END, f"Net Connection ID: {interface.NetConnectionID}\n\n")
Retrieving Graphics Processing Units (GPU) Information
Similar to the sections above, this part goes through each detected GPU to retrieve information about them such as “the amount of memory RAM” available in gigabytes, as well as “the name of the GPU“, and also a “description of the GPU“, then insert it into the hardware_info
widget.
hardware_info.insert(tk.END, "\nGraphics Processing Units (GPUs):\n")
for gpu in c.Win32_VideoController():
hardware_info.insert(tk.END, f"Name: {gpu.Name}\n")
hardware_info.insert(tk.END, f"Adapter RAM: {gpu.AdapterRAM / (1024 ** 3):.2f} GB\n")
hardware_info.insert(tk.END, f"Caption: {gpu.Caption}\n")
hardware_info.insert(tk.END, f"Status: {gpu.Status}\n\n")
Creating the Main Window
Now, we create our main window and give it the title of ‘Hardware Information’.
# Create the main application window
root = tk.Tk()
root.title("Hardware Information - The Pycodes")
Creating the Scrolled Text Widget
After that, we create a text widget where our hardware information will be displayed, we will name it hardware_info
and make it scrollable.
# Create a scrolled text widget to display hardware info
hardware_info = scrolledtext.ScrolledText(root, width=80, height=30, wrap=tk.WORD)
hardware_info.pack(expand=True, fill="both", padx=10, pady=10)
Creating the Fetch Button
Here, we created a button with the name “Fetch Hardware Info” When the user clicks this button it will trigger the get_hardware_info
Function.
# Create a button to fetch hardware info
fetch_button = tk.Button(root, text="Fetch Hardware Info", command=get_hardware_info)
fetch_button.pack(pady=10)
Running the Main Loop
This last part is the one responsible for keeping the main window running and responsive to the user.
# Run the Tkinter event loop
root.mainloop()
Example
Full Code
import tkinter as tk
from tkinter import scrolledtext
import wmi
def get_hardware_info():
hardware_info.delete(1.0, tk.END) # Clear previous info
c = wmi.WMI()
hardware_info.insert(tk.END, "Disk Drives:\n")
for disk in c.Win32_DiskDrive():
hardware_info.insert(tk.END, f"Model: {disk.Model}\n")
hardware_info.insert(tk.END, f"Interface Type: {disk.InterfaceType}\n")
hardware_info.insert(tk.END, f"Size: {int(disk.Size) / (1024 ** 3):.2f} GB\n")
hardware_info.insert(tk.END, f"Serial Number: {disk.SerialNumber}\n")
hardware_info.insert(tk.END, f"Status: {disk.Status}\n\n")
hardware_info.insert(tk.END, "\nNetwork Adapters:\n")
for interface in c.Win32_NetworkAdapter():
hardware_info.insert(tk.END, f"Name: {interface.Name}\n")
hardware_info.insert(tk.END, f"Description: {interface.Description}\n")
hardware_info.insert(tk.END, f"MAC Address: {interface.MACAddress}\n")
if interface.Speed is not None:
if interface.Speed.isdigit(): # Check if speed is a digit
hardware_info.insert(tk.END,
f"Speed: {int(interface.Speed) / 10 ** 6} Mbps\n") # Convert to integer before division
else:
hardware_info.insert(tk.END, f"Speed: Unknown\n")
else:
hardware_info.insert(tk.END, f"Speed: Unknown\n")
hardware_info.insert(tk.END, f"Manufacturer: {interface.Manufacturer}\n")
hardware_info.insert(tk.END, f"Net Connection ID: {interface.NetConnectionID}\n\n")
hardware_info.insert(tk.END, "\nGraphics Processing Units (GPUs):\n")
for gpu in c.Win32_VideoController():
hardware_info.insert(tk.END, f"Name: {gpu.Name}\n")
hardware_info.insert(tk.END, f"Adapter RAM: {gpu.AdapterRAM / (1024 ** 3):.2f} GB\n")
hardware_info.insert(tk.END, f"Caption: {gpu.Caption}\n")
hardware_info.insert(tk.END, f"Status: {gpu.Status}\n\n")
# Create the main application window
root = tk.Tk()
root.title("Hardware Information - The Pycodes")
# Create a scrolled text widget to display hardware info
hardware_info = scrolledtext.ScrolledText(root, width=80, height=30, wrap=tk.WORD)
hardware_info.pack(expand=True, fill="both", padx=10, pady=10)
# Create a button to fetch hardware info
fetch_button = tk.Button(root, text="Fetch Hardware Info", command=get_hardware_info)
fetch_button.pack(pady=10)
# Run the Tkinter event loop
root.mainloop()
Happy Coding!