Using psutil and tkinter, you can create a GUI in Python that displays information about the computer’s CPU, memory, disk, and battery.
Here’s a code example that uses psutil to gather this information and tkinter to display it in a GUI window.
Code
import tkinter as tk
import psutil
# Function to update the displayed system information
def update_info():
# Get CPU information
cpu_percent = psutil.cpu_percent(interval=1)
cpu_label.config(text=f"CPU Usage: {cpu_percent}%")
# Get Memory information
memory_info = psutil.virtual_memory()
memory_label.config(text=f"Memory Usage: {memory_info.percent}%")
# Get Disk information
disk_info = psutil.disk_usage('/')
disk_label.config(text=f"Disk Usage: {disk_info.percent}%")
# Get Battery information
battery = psutil.sensors_battery()
if battery:
battery_label.config(text=f"Battery: {battery.percent}% {'(Plugged In)' if battery.power_plugged else '(Not Plugged In)'}")
else:
battery_label.config(text="Battery: No battery found")
# Schedule the update_info function to run again after 1000 milliseconds (1 second)
root.after(1000, update_info)
# Set up the main application window
root = tk.Tk()
root.title("System Information")
root.geometry("300x200")
# Labels for displaying information
cpu_label = tk.Label(root, text="CPU Usage: ", font=("Helvetica", 12))
cpu_label.pack(pady=5)
memory_label = tk.Label(root, text="Memory Usage: ", font=("Helvetica", 12))
memory_label.pack(pady=5)
disk_label = tk.Label(root, text="Disk Usage: ", font=("Helvetica", 12))
disk_label.pack(pady=5)
battery_label = tk.Label(root, text="Battery: ", font=("Helvetica", 12))
battery_label.pack(pady=5)
# Call update_info() function to populate the initial data and start updating periodically
update_info()
# Run the main loop
root.mainloop()
Explanation
- Import Required Libraries:
- tkinter is used to create the GUI elements (labels, window).
- psutil is a Python library for retrieving information on system usage, including CPU, memory, disk, and battery.
- Define the update_info() Function:
- This function gathers system information using psutil and updates the labels in the GUI.
- CPU Usage:
- cpu_percent = psutil.cpu_percent(interval=1): Gets the CPU usage percentage over 1 second.
- cpu_label.config(text=f”CPU Usage: {cpu_percent}%”): Updates the cpu_label with the current CPU usage.
- Memory Usage:
- memory_info = psutil.virtual_memory(): Gets memory usage information.
- memory_label.config(text=f”Memory Usage: {memory_info.percent}%”): Displays the memory usage percentage.
- Disk Usage:
- disk_info = psutil.disk_usage(‘/’): Gets disk usage for the root directory (/).
- disk_label.config(text=f”Disk Usage: {disk_info.percent}%”): Updates disk_label with the disk usage percentage.
- Battery Information:
- battery = psutil.sensors_battery(): Checks battery status. It returns None if no battery is present.
- If a battery is detected, battery_label.config(…) updates the label with the battery percentage and charging status (Plugged In or Not Plugged In).
- If no battery is detected, the label displays “Battery: No battery found.”
- Repeat the Update:
- root.after(1000, update_info): Schedules update_info to run again after 1000 milliseconds (1 second), creating a loop to refresh the information every second.
- Set Up the Main Application Window:
- root = tk.Tk() initializes the main window.
- root.title(“System Information”) sets the window title.
- root.geometry(“300×200”) sets the window dimensions to 300×200 pixels.
- Create Labels for Displaying System Information:
- cpu_label, memory_label, disk_label, and battery_label are Label widgets used to display CPU, memory, disk, and battery information.
- font=(“Helvetica”, 12) sets a readable font size for each label.
- .pack(pady=5) adds some padding between each label for better spacing.
- Start Updating the Information:
- update_info() is called once to initialize the display and starts the periodic updates.
- Run the Main Event Loop:
- root.mainloop() keeps the GUI window open and responsive, allowing it to update every second.
Sample Output
The GUI will display real-time information about the system’s resources, similar to the following:
CPU Usage: 25% Memory Usage: 65% Disk Usage: 45% Battery: 75% (Plugged In)
This information updates every second, giving you a live view of the system’s performance and battery status.
Notes
- Real-Time Monitoring: This script periodically updates system information every second.
- Error Handling: This example does not have advanced error handling. In a production environment, consider handling exceptions, especially for psutil calls.
- Battery Detection: If there’s no battery (e.g., on desktops), psutil.sensors_battery() will return None, so we check for this and display an appropriate message.
Summary
This code demonstrates how to use psutil and tkinter to create a simple, real-time GUI application that monitors system information, including CPU, memory, disk, and battery.
It’s a useful tool for quickly viewing system resource usage without using command-line tools or a task manager.
Explore the psutil library and you can add a lot of additional information to your basic gui example