SoFunction
Updated on 2025-04-14

Check CPU model with Python and pop up warning message

Tutorial Objectives

This tutorial will guide you on how to write a Python program that is able to check the CPU model of your computer at startup. If the CPU model is detected to contain "I3", a warning window will pop up, prompting the user that the computer is performing poorly and may not be suitable for running this software. If the CPU model does not contain "I3", the program will continue to run.

Method 1

Required library

  • cpuinfo: Used to obtain CPU details.
  • tkinter: Python's standard GUI library for creating graphical user interfaces, including pop-ups.

Step 1: Install the required library

First, you need to make sure that it is installedcpuinfolibrary. If it has not been installed, you can install it through the following command:

pip install cpuinfo

tkinteris a standard library for Python, so no additional installation is required.

Step 2: Write a Python program

Next, we will write a Python program to implement the above functions.

  • Create a new Python file, e.g.check_cpu.py

  • Import the required libraries in the file.

import cpuinfo
import tkinter as tk
from tkinter import messagebox
  • Define a functioncheck_cpu_and_show_message, used to check CPU model and display corresponding messages.
def check_cpu_and_show_message():
    # Get CPU information    cpu_info = cpuinfo.get_cpu_info()
    cpu_brand = cpu_info.get('brand_raw', '').upper()  # Convert to uppercase for case-insensitive comparison
    # Determine whether the CPU model contains "I3"    if "I3" in cpu_brand:
        # Create the main window (although the main window is not required to be displayed here, messagebox requires a tkinter environment)        root = ()
        ()  # Hide the main window        
        # Pop-up window prompts        ("warn", "The computer performance is low, it is not recommended to use this software")
        
        # Exit the program (optional, decide whether to exit according to your needs)        ()
        exit(1)  # Exit code is non-zero to indicate abnormal exit
    # If the CPU is not I3, continue to perform subsequent operations (you can add your program logic here)    print("The CPU check passes, the program continues to run...")
    # Subsequent program logic... (Omitted here, you can add it as needed)
  • At the bottom of the file, add code to call the function when the program starts.
if __name__ == "__main__":
    check_cpu_and_show_message()

Step 3: Run the program

Now you can run this program. On the command line or terminal, navigate to includecheck_cpu.pyFile directory and enter the following command:

python check_cpu.py

If your CPU model contains "I3", you will see a warning pop-up. If the CPU model does not contain "I3", you will see a message "CPU check passes, the program continues to run..." on the command line, and the program will continue to execute (if there is subsequent logic).

Things to note

  • The code in this tutorial is only used as an example and may need to be adjusted according to the specific needs when actually using it.
  • The messages in the pop-up window are hardcoded, and you can modify the message content as needed.
  • The part that exits the program is optional, and you can decide whether to exit the program based on actual needs.
  • If your program needs to run in a GUI environment and you do not want to see the hidden tkinter main window, you can make sure that it is destroyed immediately after the popup, as shown in the example code.

Method 2 (Windows platform)

Import the necessary modules

At the beginning of the script, we need to importsubprocessmodule to run system commands, andtkinterandTo create a graphical user interface (GUI) warning window.

import subprocess
import tkinter as tk
from tkinter import messagebox

Define the function to get the CPU model

Write a functionget_cpu_model_windows(), this function usessubprocess.check_output()Come to runwmic cpu get Namecommand, and parse the output to get the CPU model.

def get_cpu_model_windows():
    try:
        result = subprocess.check_output(['wmic', 'cpu', 'get', 'Name'], stderr=, text=True)
        lines = ().split('\n')
        return lines
    except  as e:
        print(f"runwmicAn error occurred during command: {}")
        return ['Unknown']

Notice:becausewmicThe output of the command may contain multiple lines, and we return a list of all lines.

Define a function that checks CPU model and displays warnings

Write a functioncheck_cpu_model(), this function callget_cpu_model_windows()to get the CPU model list and iterate over the list to check if "I3" is included. If included, callshow_warning()The function displays a warning window.

def check_cpu_model():
    cpu_model = get_cpu_model_windows()
    for i in cpu_model:
        if 'i3' in ():
            show_warning()
            break  # Once the row containing "I3" is found, the loop breaks out    else:
        print("The CPU model does not include 'I3' and the program will continue to run.")
        # Add your program logic here

Notice:We usebreakStatement to exit the loop after finding a match, avoiding unnecessary iteration.

Define a function that displays a warning window

Write a functionshow_warning(), this function usestkinterCreate a simple warning window.

def show_warning():
    root = ()
    ()  # Hide the main window    ("warn", "This computer has low performance and is not recommended to use this software.")
    ()  # Destroy Tkinter instance

Run the program

At the end of the script, addif __name__ == "__main__":Statement to callcheck_cpu_model()function.

if __name__ == "__main__":
    check_cpu_model()

Execute scripts

savecheck_cpu.pyFile, and then run it on the command line:

python check_cpu.py

If the CPU model contains "I3", you will see a warning window. Otherwise, you will see a message indicating that the program will continue to run.

Things to note

  • This tutorial is for Windows platform. If you are using Linux or macOS, you need to modify the function to get the CPU model to adapt to your operating system.
  • In actual development, you may want to integrate the warning logic more closely with the rest of the program instead of simply printing a message.
  • You can customize the text and title of the warning window as needed.

Through this tutorial, you should be able to learn how to write a simple Python program to check CPU models.

This is the article about using Python to check CPU model and pop up warning information. For more related Python to check CPU model and pop up warning content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!