Skip to content

Code Explaination PCOptimus.py

truelockmc edited this page Sep 5, 2024 · 1 revision

Here is a detailed Explaination for the Code in PCOptimus.py

from tkinter import messagebox, ttk
from speedtest import Speedtest, ConfigRetrievalError
import psutil
import tkinter as tk
import tkinter.messagebox as messagebox
import threading
import platform
import socket
import time
import subprocess
import os
import re
import chardet
import ctypes
import sys
import traceback
import logging
import zipfile
import requests
import win32com.client

1. Import Statements Overview

The provided code snippet includes multiple import statements, each serving a specific purpose in the program. These import statements bring in various libraries and modules that are essential for implementing different features of the application.

2. Detailed Explanation of Each Import

  1. from tkinter import messagebox, ttk

    • tkinter: A built-in Python module for creating graphical user interfaces (GUIs). It provides various widgets such as buttons, labels, text boxes, etc.
    • messagebox: A submodule of tkinter that provides dialog boxes for displaying error messages, warnings, information, and confirmation dialogs.
    • ttk: Another submodule of tkinter, offering enhanced widgets that provide a more modern look and additional functionalities, such as ttk.Button, ttk.Label, ttk.Combobox, etc.
  2. from speedtest import Speedtest, ConfigRetrievalError

    • speedtest: A module used to test internet connection speed. It provides functionalities to measure download speed, upload speed, and ping time.
    • Speedtest: A class within the speedtest module that is used to perform the speed tests.
    • ConfigRetrievalError: An exception class from the speedtest module, raised when configuration settings for the speed test cannot be retrieved.
  3. import psutil

    • psutil: A module for system and process utilities. It is used to retrieve information on system utilization (CPU, memory, disks, network, sensors) and running processes.
  4. import tkinter as tk

    • Another import statement for tkinter, imported here with the alias tk to simplify access to its functions.
  5. import tkinter.messagebox as messagebox

    • This is a redundant import since messagebox has already been imported above. However, it is imported again here with an alias messagebox for direct access to functions like messagebox.showinfo() or messagebox.showerror().
  6. import threading

    • threading: A module that provides support for multithreading. It allows concurrent execution of multiple threads, which is helpful for performing background tasks without blocking the main application.
  7. import platform

    • platform: A module that provides access to information about the underlying operating system and Python interpreter version. It can be used to retrieve platform-specific details.
  8. import socket

    • socket: A module for low-level networking interface. It enables the creation and use of network connections to send and receive data over TCP/IP protocols.
  9. import time

    • time: A module that provides time-related functions, such as retrieving the current time (time.time()), pausing execution (time.sleep()), timestamping, and time conversions.
  10. import subprocess

    • subprocess: A module that allows the execution of new processes, the ability to control their input and output, and retrieve return codes. It is commonly used to run external programs or issue system commands.
  11. import os

    • os: A module that provides functions for interacting with the operating system, such as file system operations (create, remove, rename files and directories), environment variables, and process management.
  12. import re

    • re: A module for regular expressions used for text searching and manipulation. It allows for pattern matching and substitution within strings.
  13. import chardet

    • chardet: A module for automatic character encoding detection. It is useful when dealing with text data whose encoding is unknown.
  14. import ctypes

    • ctypes: A module that allows Python to interact with C libraries. It can be used to call C functions and manipulate C data types directly from Python.
  15. import sys

    • sys: A module that provides functions and variables used to manipulate different parts of the Python runtime environment. It can be used to access command-line arguments (sys.argv), exit the program (sys.exit()), modify the Python path (sys.path), etc.
  16. import traceback

    • traceback: A module useful for debugging. It provides functionalities to print or format stack traces of Python errors, which is helpful for error analysis.
  17. import logging

    • logging: A module that provides a flexible framework for embedding log messages in applications. It can be used to log errors, warnings, debug information, and other messages.
  18. import zipfile

    • zipfile: A module that supports reading and writing ZIP archives. It allows compressing files into a ZIP format and extracting files from a ZIP archive.
  19. import requests

    • requests: A popular module for making HTTP requests. It simplifies interaction with web APIs and websites by providing a simple interface for sending GET, POST, PUT, DELETE requests, and more.
  20. import win32com.client

    • win32com.client: A module from the pywin32 library that enables Python to interact with Windows COM (Component Object Model) objects. This allows automation of Windows applications like Microsoft Office (Excel, Word, Outlook) through their COM interfaces. It is useful for automating tasks such as creating Word documents, sending emails via Outlook, or manipulating Excel spreadsheets directly from Python. In this Code it is used to create Costum shortcuts.

3. Summary

The code imports a variety of modules and packages, each providing different functionalities required by the program, ranging from GUI creation, system and network operations to file manipulation and web requests. These imports set the foundation for a program capable of performing multiple tasks such as gathering system information, testing network speed, interacting with users, manipulating files, and executing external processes.


Code Explanation for Logging Configuration and Script Directory Retrieval

This section of the code focuses on configuring the logging system for the application and determining the directory in which the script is located.

1. Configuring the Logging System

logging.basicConfig(filename="log.txt", level=logging.INFO, 
                    format="%(asctime)s - %(levelname)s - %(message)s")
  • logging.basicConfig: This function configures the logging system in Python. Logging is a means of tracking events that happen when some software runs. The basicConfig function sets up the basic configuration for the logging system.

    • filename="log.txt": Specifies the name of the file where log messages should be stored. In this case, logs will be written to a file named log.txt in the current working directory. If the file does not exist, it will be created. If it exists, new log messages will be appended to the end of the file.

    • level=logging.INFO: Sets the logging level to INFO. The logging levels indicate the severity of the events to be logged. In Python's logging module, these levels are, in increasing order of severity: DEBUG, INFO, WARNING, ERROR, and CRITICAL. By setting the level to INFO, the logger will handle all messages that are INFO level or higher (i.e., INFO, WARNING, ERROR, CRITICAL).

    • format="%(asctime)s - %(levelname)s - %(message)s": Specifies the format of the log messages.

      • %(asctime)s: Inserts the timestamp when the log entry was created.
      • %(levelname)s: Inserts the level of the log message (e.g., INFO, ERROR).
      • %(message)s: Inserts the actual log message.

    This configuration will log messages in a format similar to:

    2024-08-25 12:34:56,789 - INFO - This is an informational message.
    

2. Retrieving the Directory of the Script

# Get the directory of the script
script_dir = os.path.dirname(os.path.abspath(__file__))
  • os.path.abspath(__file__): This part of the code retrieves the absolute path of the current script file (__file__). The __file__ variable is a special Python variable that contains the path to the script being executed. When passed to os.path.abspath(), it returns the absolute path of the script.

  • os.path.dirname(...): This function is used to extract the directory path from the absolute path of the script file. Given a full path, os.path.dirname() returns the directory part of the path, effectively removing the script's filename.

  • script_dir: This variable will hold the directory path where the current script is located. This is useful when the script needs to access files in the same directory or perform directory-related operations.

Summary

The provided code snippet sets up basic logging for an application, directing log messages of INFO level and higher to a file named log.txt with a specific format that includes timestamps, log levels, and the log message. Additionally, the code determines the directory where the script is located, which can be helpful for handling file paths relative to the script's location.


Code Explanation for Checking Administrative Privileges

This section of the code defines a function to determine if the current user has administrative privileges on a Windows system.

Function: is_admin()

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

Purpose:
The is_admin() function checks if the script is being executed with administrative privileges. This is particularly important for operations that require elevated permissions, such as modifying system settings or accessing protected files.

Detailed Explanation:

  1. Import Requirement:

    • The function relies on the ctypes module, which allows calling functions in DLLs/shared libraries. ctypes is a foreign function library for Python, providing C compatible data types and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
  2. ctypes.windll.shell32.IsUserAnAdmin():

    • ctypes.windll: This is an object provided by ctypes that allows access to Windows API functions.
    • shell32: A Windows system DLL that contains Windows Shell API functions, which are used to interact with the Windows shell.
    • IsUserAnAdmin(): This function is a part of the shell32 DLL and returns True if the user has administrative privileges; otherwise, it returns False. It checks whether the user context of the running process belongs to the Administrators group.
  3. try Block:

    • The call to IsUserAnAdmin() is wrapped in a try block to handle any potential exceptions that might occur, such as if the function is not available on the platform or if there is an issue accessing the Windows API.
  4. except Block:

    • If an exception is raised (for example, if the script is not running on Windows or there is a permissions issue), the except block catches the exception, and the function returns False. This ensures that the function gracefully handles errors without crashing the script.
  5. Return Values:

    • True: If the user has administrative privileges.
    • False: If the user does not have administrative privileges or if an exception occurs during the check.

Usage Example

This function can be used to check for administrative privileges before performing an operation that requires elevated rights. For example:

if not is_admin():

### **Code Explanation for Checking Administrative Privileges**

This section of the code defines a function to determine if the current user has administrative privileges on a Windows system.

#### **Function: `is_admin()`**

```python
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

Summary

The is_admin() function is a utility that checks if the current script is being run with administrative privileges on a Windows system. It leverages the ctypes module to call a Windows API function, and handles potential errors gracefully by returning False if an error occurs or if the user lacks the necessary privileges.


Code Explanation for Shortcut Creation

def create_shortcut():
    # Automatische Bestimmung des aktuellen Skript-Pfads
    script_path = os.path.abspath(__file__)

    # Python-Interpreter-Pfad
    python_exe = sys.executable

    # Befehl zum Ausführen des Skripts mit dem Python-Interpreter
    command = f'"{python_exe}" "{script_path}"'

    # Name der Verknüpfung
    shortcut_name = 'PCOptimus'

    # Speicherort der Verknüpfung im Startmenü
    start_menu_path = os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Start Menu', 'Programs')
    shortcut_path = os.path.join(start_menu_path, f"{shortcut_name}.lnk")

    # Überprüfen, ob die Verknüpfung bereits existiert
    if os.path.exists(shortcut_path):
        print(f"The shortcut '{shortcut_name}' already exists at: {shortcut_path}")
    else:
        try:
            # Erstellen der Verknüpfung mit win32com.client
            shell = win32com.client.Dispatch('WScript.Shell')
            shortcut = shell.CreateShortcut(shortcut_path)
            shortcut.TargetPath = python_exe
            shortcut.Arguments = f'"{script_path}"'
            shortcut.WorkingDirectory = os.path.dirname(script_path)
            shortcut.IconLocation = python_exe
            shortcut.save()

            print(f"Shortcut '{shortcut_name}' successfully created at: {shortcut_path}")

            # Benachrichtigung und Abfrage, ob eine Desktop-Verknüpfung erstellt werden soll
            root = tk.Tk()
            root.withdraw()  # Verstecke das Hauptfenster

            # Zeige eine Nachricht an den Benutzer
            messagebox.showinfo("PCOptimus Shortcut Created",
                                "PCOptimus is now available in the Start Menu.\n")

            # Benutzerabfrage für die Desktop-Verknüpfung
            if messagebox.askyesno("Create Desktop Shortcut?",
                                   "Do you want to create a Desktop shortcut for PCOptimus?"):
                desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
                desktop_shortcut_path = os.path.join(desktop_path, f"{shortcut_name}.lnk")

                # Überprüfen, ob die Desktop-Verknüpfung bereits existiert
                if not os.path.exists(desktop_shortcut_path):
                    # Erstellen der Desktop-Verknüpfung
                    desktop_shortcut = shell.CreateShortcut(desktop_shortcut_path)
                    desktop_shortcut.TargetPath = python_exe
                    desktop_shortcut.Arguments = f'"{script_path}"'
                    desktop_shortcut.WorkingDirectory = os.path.dirname(script_path)
                    desktop_shortcut.IconLocation = python_exe
                    desktop_shortcut.save()
                    
                    messagebox.showinfo("Desktop Shortcut Created",
                                        f"Desktop shortcut '{shortcut_name}' successfully created.")
                else:
                    messagebox.showinfo("Desktop Shortcut Exists",
                                        f"The desktop shortcut '{shortcut_name}' already exists.")
        except Exception as e:
            messagebox.showerror("Error Creating Shortcut", f"Error creating the shortcut: {e}")

1. Defining the Function to Create a Shortcut

def create_shortcut():
  • create_shortcut(): This function is designed to automate the process of creating a shortcut for the current Python script. It configures all the necessary settings for placing a shortcut in the Windows Start Menu and optionally on the desktop.

2. Determining the Script and Python Interpreter Path

    script_path = os.path.abspath(__file__)
    python_exe = sys.executable
    command = f'"{python_exe}" "{script_path}"'
  • script_path = os.path.abspath(__file__): This command retrieves the absolute path of the script currently being executed. The __file__ variable in Python holds the filename of the script, and os.path.abspath() converts this to a complete path, including the directory.

  • python_exe = sys.executable: This retrieves the full path to the Python interpreter being used to run the script. The sys.executable ensures that the same interpreter is used when the shortcut runs.

  • command = f'"{python_exe}" "{script_path}"': This command constructs the full string that will be executed when the shortcut is clicked. It combines the Python interpreter and the script path into a single command.

3. Defining the Shortcut Name and Location

    shortcut_name = 'PCOptimus'
    start_menu_path = os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Start Menu', 'Programs')
    shortcut_path = os.path.join(start_menu_path, f"{shortcut_name}.lnk")
  • shortcut_name = 'PCOptimus': This defines the name of the shortcut to be created. In this case, the shortcut will be named PCOptimus.

  • start_menu_path = os.path.join(...): This constructs the path to the Start Menu's Programs folder by combining the environment variable APPDATA with the Microsoft Windows Start Menu folder structure. The os.environ retrieves environment variables, and os.path.join() safely concatenates paths.

  • shortcut_path = os.path.join(...): This appends the shortcut name and file extension (.lnk) to the Start Menu path, defining where the shortcut file will be saved.

4. Checking if the Shortcut Already Exists

    if os.path.exists(shortcut_path):
        print(f"The shortcut '{shortcut_name}' already exists at: {shortcut_path}")
  • os.path.exists(shortcut_path): This checks whether a file or shortcut with the given name already exists at the specified path. If it does, a message is printed, and the function does not create a new shortcut.

5. Creating the Shortcut with win32com.client

    else:
        try:
            shell = win32com.client.Dispatch('WScript.Shell')
            shortcut = shell.CreateShortcut(shortcut_path)
            shortcut.TargetPath = python_exe
            shortcut.Arguments = f'"{script_path}"'
            shortcut.WorkingDirectory = os.path.dirname(script_path)
            shortcut.IconLocation = python_exe
            shortcut.save()
  • shell = win32com.client.Dispatch('WScript.Shell'): This line uses the win32com.client module to interact with Windows' COM interface, specifically with WScript.Shell, which provides methods to create shortcuts.

  • shortcut = shell.CreateShortcut(shortcut_path): Creates a new shortcut object at the specified path.

  • shortcut.TargetPath = python_exe: Sets the target executable of the shortcut, which is the Python interpreter.

  • shortcut.Arguments = f'"{script_path}"': This passes the script's path as an argument to the Python interpreter, ensuring that the shortcut runs the correct script.

  • shortcut.WorkingDirectory = os.path.dirname(script_path): Sets the working directory to the script's directory, which ensures that any relative file paths used within the script will function properly.

  • shortcut.IconLocation = python_exe: Assigns the Python executable as the icon for the shortcut.

  • shortcut.save(): Saves the shortcut to disk at the specified location.

6. Prompting the User to Create a Desktop Shortcut

            root = tk.Tk()
            root.withdraw()
            messagebox.showinfo("PCOptimus Shortcut Created", "PCOptimus is now available in the Start Menu.\n")
            if messagebox.askyesno("Create Desktop Shortcut?", "Do you want to create a Desktop shortcut for PCOptimus?"):
                desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
                desktop_shortcut_path = os.path.join(desktop_path, f"{shortcut_name}.lnk")
  • root = tk.Tk(): Initializes the Tkinter GUI system. This is required to create the message box, but the main window is hidden with root.withdraw() to keep it from appearing.

  • messagebox.showinfo(...): A pop-up dialog informs the user that the Start Menu shortcut has been created successfully.

  • messagebox.askyesno(...): This dialog prompts the user to confirm if they want to create a Desktop shortcut as well.

  • desktop_path = os.path.join(...): This generates the full path to the user's Desktop folder by combining the user's home directory (retrieved using os.path.expanduser("~")) with the Desktop folder name.

7. Creating the Desktop Shortcut if Confirmed by the User

                if not os.path.exists(desktop_shortcut_path):
                    desktop_shortcut = shell.CreateShortcut(desktop_shortcut_path)
                    desktop_shortcut.TargetPath = python_exe
                    desktop_shortcut.Arguments = f'"{script_path}"'
                    desktop_shortcut.WorkingDirectory = os.path.dirname(script_path)
                    desktop_shortcut.IconLocation = python_exe
                    desktop_shortcut.save()
                    
                    messagebox.showinfo("Desktop Shortcut Created",
                                        f"Desktop shortcut '{shortcut_name}' successfully created.")
                else:
                    messagebox.showinfo("Desktop Shortcut Exists",
                                        f"The desktop shortcut '{shortcut_name}' already exists.")
  • if not os.path.exists(desktop_shortcut_path): This checks if a shortcut already exists on the Desktop. If

it does not, the shortcut is created.

  • desktop_shortcut = shell.CreateShortcut(desktop_shortcut_path): Similar to the Start Menu shortcut creation process, this creates a Desktop shortcut following the same process of setting the TargetPath, Arguments, WorkingDirectory, and IconLocation.

  • messagebox.showinfo(...): Informs the user whether the Desktop shortcut was successfully created or if it already exists.

8. Handling Exceptions

        except Exception as e:
            messagebox.showerror("Error Creating Shortcut", f"Error creating the shortcut: {e}")
  • except Exception as e: This catches any errors that may occur during the shortcut creation process. If an error is encountered, a message box will display the error message, helping the user understand what went wrong.

Summary

This function provides a complete automation solution for creating shortcuts in both the Start Menu and Desktop on Windows systems, making it easier for users to access and run the script.


Code Explanation for Cleaning the Recycle Bin

This section of the code defines a function to clean (empty) the Recycle Bin on a Windows system. It checks the operating system and, if it is Windows, uses a system call to perform the clean-up. The function also provides user feedback and error handling.

Function: clean_recycle_bin()

def clean_recycle_bin():
    try:
        if platform.system() == "Windows":
            # Alternative approach using ctypes
            ctypes.windll.shell32.SHEmptyRecycleBinW(None, None, 0x0007)
            messagebox.showinfo("Success", "Recycle Bin cleaned successfully.")
        else:
            messagebox.showwarning("Warning", "This operation is only supported on Windows.")
    except Exception as e:
        log_error("Failed to clean recycle bin", e)
        messagebox.showerror("Error", "An error occurred while cleaning the recycle bin.")

Purpose:
The clean_recycle_bin() function is designed to empty the Recycle Bin on a Windows system. It ensures that the operation is only attempted on a supported platform (Windows) and provides user feedback based on the outcome.

Detailed Explanation:

  1. Platform Check:

    • platform.system(): This function returns the name of the operating system dependent on the platform the script is being executed on. Possible return values include 'Windows', 'Linux', 'Darwin' (for macOS), and others.
    • if platform.system() == "Windows":: The function first checks if the operating system is Windows. This operation (emptying the Recycle Bin using ctypes) is only supported on Windows. If the system is not Windows, it shows a warning message.
  2. Emptying the Recycle Bin:

    • ctypes.windll.shell32.SHEmptyRecycleBinW(None, None, 0x0007):
      • ctypes.windll.shell32: This provides access to functions from the shell32 DLL, which contains many shell functions, including those for interacting with the Recycle Bin.
      • SHEmptyRecycleBinW: This is a Windows API function that empties the Recycle Bin. The W at the end signifies that it uses wide characters (Unicode).
      • Parameters:
        • None: The first parameter represents the window handle that the function should use. None means it uses no specific window.
        • None: The second parameter specifies the drive to empty the Recycle Bin on. None means it empties the Recycle Bin on all drives.
        • 0x0007: This is a combination of flags:
          • SHERB_NOCONFIRMATION (0x0001): No confirmation dialog should be shown.
          • SHERB_NOPROGRESSUI (0x0002): No progress UI should be displayed.
          • SHERB_NOSOUND (0x0004): No sound should be played when the operation completes.
        • Combined (0x0007), these flags indicate that the Recycle Bin should be emptied silently (without user confirmation, progress display, or sound).
  3. User Feedback:

    • messagebox.showinfo("Success", "Recycle Bin cleaned successfully."): If the Recycle Bin is emptied successfully, an information message box is shown to the user, confirming the successful operation.

    • messagebox.showwarning("Warning", "This operation is only supported on Windows."): If the script detects that it is not running on a Windows platform, a warning message box is displayed to inform the user that the operation is not supported.

  4. Error Handling:

    • The entire operation is wrapped in a try block to catch any exceptions that may occur during the process.
    • except Exception as e:: Catches any exceptions that occur during the attempt to empty the Recycle Bin.
    • log_error("Failed to clean recycle bin", e): Calls a logging function (presumably defined elsewhere in the code) to log the error message and exception details. This helps in debugging and tracking issues.
    • messagebox.showerror("Error", "An error occurred while cleaning the recycle bin."): Shows an error message box to the user if an exception occurs, indicating that the operation failed.

Usage Example

This function can be invoked in a script or application to provide a utility for users to clean their Recycle Bin directly from the application interface.

# Example usage
clean_recycle_bin()

Summary

The clean_recycle_bin() function is a utility designed to empty the Recycle Bin on a Windows operating system. It checks the platform before attempting the operation and provides feedback to the user through message boxes. The function also includes robust error handling to manage any exceptions that may occur during the process, ensuring that the application remains stable and informative to the user.


Code Explanation for Cleaning Virtual Storage (Memory)

This section of the code defines a function to clean up or reduce the virtual memory usage (working set size) of the current process on a Windows system. It checks the operating system and, if it is Windows, uses a system call to perform the clean-up. The function also provides user feedback and error handling.

Function: clean_vs()

def clean_vs():
    try:
        if platform.system() == "Windows":
            # Set the process working set size to clean virtual memory
            ctypes.windll.kernel32.SetProcessWorkingSetSize(-1, -1, -1)
            messagebox.showinfo("Success", "Virtual Storage cleaned successfully.")
        else:
            messagebox.showwarning("Warning", "This operation is only supported on Windows.")
    except Exception as e:
        log_error("Failed to clean virtual Storage", e)
        messagebox.showerror("Error", f"An error occurred while cleaning the virtual Storage: {str(e)}")

Purpose:
The clean_vs() function is designed to reduce the memory footprint of the current process by adjusting its working set size. This is particularly useful in freeing up virtual memory that is no longer needed, effectively "cleaning" the virtual memory used by the process.

Detailed Explanation:

  1. Platform Check:

    • platform.system(): This function returns the name of the operating system on which the script is running.
    • if platform.system() == "Windows":: The function first checks if the operating system is Windows. The operation to clean virtual memory using ctypes is only supported on Windows.
  2. Cleaning Virtual Storage (Memory):

    • ctypes.windll.kernel32.SetProcessWorkingSetSize(-1, -1, -1):
      • ctypes.windll: Provides access to Windows API functions.
      • kernel32: A core Windows DLL that contains many system-level functions, including memory management functions.
      • SetProcessWorkingSetSize: This function is a Windows API call that sets the minimum and maximum working set size for a specified process.
        • Working Set: The working set of a process is the set of memory pages currently visible to the process in physical RAM; they are resident and available for an application to use without triggering a page fault.
        • Parameters:
          • The three -1 parameters indicate that the function should use the default values for minimum and maximum working set sizes, effectively trimming the working set and returning as much memory as possible to the system. This is a way to prompt Windows to reduce the memory usage of the current process.
    • User Feedback:
      • messagebox.showinfo("Success", "Virtual Storage cleaned successfully."): If the working set size is successfully adjusted, an information message box is shown to the user, confirming the successful operation.
  3. Non-Windows Platform Handling:

    • else:: If the script detects that it is not running on a Windows platform, a warning message box is displayed to inform the user that the operation is not supported.
    • messagebox.showwarning("Warning", "This operation is only supported on Windows."): Displays a warning to the user if the operating system is not Windows.
  4. Error Handling:

    • The entire operation is wrapped in a try block to catch any exceptions that may occur during the process.
    • except Exception as e:: Catches any exceptions that occur during the attempt to adjust the working set size.
    • log_error("Failed to clean virtual Storage", e): Calls a logging function (presumably defined elsewhere in the code) to log the error message and exception details. This helps in debugging and tracking issues.
    • messagebox.showerror("Error", f"An error occurred while cleaning the virtual Storage: {str(e)}"): Shows an error message box to the user if an exception occurs, indicating that the operation failed and providing the exception message.

Usage Example

This function can be invoked in a script or application to provide a utility for users to clean virtual memory directly from the application interface.

# Example usage
clean_vs()

Summary

The clean_vs() function is a utility designed to reduce the working set size of the current process on a Windows operating system, effectively freeing up virtual memory that is no longer needed. It checks the platform before attempting the operation and provides feedback to the user through message boxes. The function also includes robust error handling to manage any exceptions that may occur during the process, ensuring that the application remains stable and informative to the user.


Code Explanation for Disk Defragmentation Function

This section of the code defines a function to initiate a disk defragmentation process on a Windows system. It checks the operating system and, if it is Windows, uses a system command to start the defragmentation utility. The function provides user feedback and error handling.

Function: defragment()

def defragment():
    try:
        if platform.system() == "Windows":
            run_command("dfrgui.exe", "Defragmentation completed successfully.")
        else:
            messagebox.showwarning("Warning", "This operation is only supported on Windows.")
    except Exception as e:
        log_error("Failed to defragment", e)
        messagebox.showerror("Error", "An error occurred while defragmenting the disk.")

Purpose:
The defragment() function is designed to initiate the built-in Windows Disk Defragmenter tool to optimize disk performance by rearranging fragmented data on the hard drive. It ensures that the operation is only performed on a supported platform (Windows) and provides user feedback based on the outcome.

Detailed Explanation:

  1. Platform Check:

    • platform.system(): This function returns the name of the operating system on which the script is running.
    • if platform.system() == "Windows":: The function first checks if the operating system is Windows. The defragmentation operation using the Windows utility is only supported on Windows systems.
  2. Running the Defragmentation Command:

    • run_command("dfrgui.exe", "Defragmentation completed successfully."):
      • This function call attempts to run the Windows Disk Defragmenter utility (dfrgui.exe), which is the graphical user interface for defragmenting and optimizing drives.
      • run_command(command, success_message): This is a placeholder function that is assumed to execute a system command and then display a success message. In this case:
        • "dfrgui.exe": The command to execute the built-in Windows Disk Defragmenter tool.
        • "Defragmentation completed successfully.": A message to display upon successful completion of the defragmentation operation.
    • Note: The actual implementation of run_command() is not provided in the snippet, so it's assumed to be a custom function defined elsewhere in the codebase that handles executing system commands and showing user messages.
  3. Non-Windows Platform Handling:

    • else:: If the script detects that it is not running on a Windows platform, a warning message box is displayed to inform the user that the operation is not supported.
    • messagebox.showwarning("Warning", "This operation is only supported on Windows."): This displays a warning to the user if the operating system is not Windows, indicating that the defragmentation tool is not available.
  4. Error Handling:

    • The entire operation is wrapped in a try block to catch any exceptions that may occur during the process.
    • except Exception as e:: Catches any exceptions that occur during the attempt to run the defragmentation command.
    • log_error("Failed to defragment", e): Calls a logging function (presumably defined elsewhere in the code) to log the error message and exception details. This helps in debugging and tracking issues.
    • messagebox.showerror("Error", "An error occurred while defragmenting the disk."): Shows an error message box to the user if an exception occurs, indicating that the operation failed.

Usage Example

This function can be invoked in a script or application to provide a utility for users to defragment their disks directly from the application interface.

# Example usage
defragment()

Summary

The defragment() function is a utility designed to initiate the disk defragmentation process on a Windows operating system. It checks the platform before attempting the operation and provides feedback to the user through message boxes. The function also includes robust error handling to manage any exceptions that may occur during the process, ensuring that the application remains stable and informative to the user.


Code Explanation for Elevating Process Privileges

This section of the code defines a function to restart the current Python script with elevated privileges (as an administrator) on a Windows system. This is useful for performing tasks that require administrative access, such as modifying system settings or accessing restricted files.

Function: elevate()

def elevate():
    # Get the path to the current Python executable
    script = os.path.abspath(sys.argv[0])
    # Restart the script as an administrator
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, f'"{script}"', None, 1)

Purpose:
The elevate() function is designed to restart the current Python script with administrative privileges if it is not already running with elevated rights. This is particularly important for operations that require higher-level access to the system.

Detailed Explanation:

  1. Get the Path to the Current Python Script:

    • os.path.abspath(sys.argv[0]):
      • sys.argv[0]: Represents the path of the script that was executed. It could be a relative path.
      • os.path.abspath(): Converts the relative path to an absolute path, ensuring the script can be found correctly regardless of the current working directory. This is necessary for the ShellExecuteW function to correctly locate the script file to restart.
  2. Restarting the Script as Administrator:

    • ctypes.windll.shell32.ShellExecuteW():
      • ctypes.windll: Provides access to functions from the Windows API.
      • shell32: A core Windows DLL that contains many shell functions, including ShellExecuteW.
      • ShellExecuteW: A Windows API function that performs an operation on a specified file. The W at the end signifies that it uses wide characters (Unicode).
      • Parameters:
        • None: The first parameter (hwnd) is a handle to the parent window. None means no window is specified.
        • "runas": The second parameter (lpOperation) specifies the operation to perform. "runas" indicates that the script should be run as an administrator.
        • sys.executable: The third parameter (lpFile) specifies the program to execute. sys.executable gives the path to the Python interpreter that is currently running the script.
        • f'"{script}"': The fourth parameter (lpParameters) specifies the parameters to pass to the executable. In this case, it is the path to the current script, wrapped in quotes to handle any spaces in the path.
        • None: The fifth parameter (lpDirectory) specifies the working directory. None means the current working directory is used.
        • 1: The last parameter (nShowCmd) specifies how the application window should be displayed. 1 means the window should be shown normally.
    • This function call attempts to restart the script with elevated privileges. If the user is not an administrator, they will be prompted to enter administrator credentials.
  3. User Privilege Elevation:

    • The function does not include any checks to see if the script is already running as an administrator. It directly attempts to restart with elevated privileges. If the script is already running with administrative privileges, the operation will effectively result in no change.

Usage Example

This function can be used in a script or application to ensure that the script is running with the required privileges.

# Example usage
if not is_admin():
    elevate()
else:
    print("Already running as administrator.")

In the above example, a check is performed using the is_admin() function (defined previously) to determine if the script is already running as an administrator. If not, the elevate() function is called to restart the script with elevated privileges.

Summary

The elevate() function is a utility that restarts the current Python script with elevated privileges (as an administrator) on a Windows system. It uses the Windows API function ShellExecuteW to prompt the user for administrator rights and restart the script. This function is crucial for performing tasks that require administrative access, such as modifying system settings or accessing restricted files.


Code Explanation for Running Commands as Administrator

This code defines two functions for executing system commands with administrative privileges, handling different scenarios including window visibility and error handling.

Function: run_admin_command(command, success_message, show_window=False)

def run_admin_command(command, success_message, show_window=False):
    try:
        if platform.system() == "Windows":
            if is_admin():
                # Execute the command directly since the user has admin rights
                creationflags = subprocess.CREATE_NO_WINDOW if not show_window else 0
                result = subprocess.run(command, shell=True, creationflags=creationflags)
                if result.returncode == 0 or result.returncode == 3:  # Ignore exit code 3
                    messagebox.showinfo("Success", success_message)
                else:
                    messagebox.showerror("Error", f"Command failed with return code {result.returncode}")
            else:
                # Restart script as administrator
                messagebox.showinfo("Info", "This action requires administrator privileges. The program will restart with elevated rights.")
                elevate()
                root.quit()  # Close the old window
                os._exit(0)  # Exit the old window immediately
        else:
            # For other operating systems: execute command normally
            result = subprocess.run(command, shell=True)
            if result.returncode == 0 or result.returncode == 3:  # Ignore exit code 3
                messagebox.showinfo("Success", success_message)
            else:
                messagebox.showerror("Error", f"Command failed with return code {result.returncode}")
    except Exception as e:
        # Log error and display message
        log_error(f"Error running admin command: {command}", e)
        messagebox.showerror("Error", f"An error occurred: {str(e)}")

Purpose:
The run_admin_command() function is designed to execute a system command with administrative privileges on Windows. It also handles whether to show a window for the command execution and provides feedback based on the success or failure of the command.

Detailed Explanation:

  1. Platform Check:

    • platform.system(): Checks if the operating system is Windows. Commands are executed with administrative rights only on Windows.
  2. Admin Check and Command Execution:

    • is_admin(): Checks if the script is running with administrative privileges.
    • subprocess.run(command, shell=True, creationflags=creationflags):
      • Executes the specified command.
      • creationflags: Uses subprocess.CREATE_NO_WINDOW to hide the command window if show_window is False. If show_window is True, the command window will be visible.
      • shell=True: Executes the command through the shell.
  3. Error Handling:

    • result.returncode == 0 or result.returncode == 3: Checks if the command was successful. Exit code 0 indicates success, and exit code 3 is ignored (often used by certain commands to indicate non-critical issues).
    • messagebox.showinfo("Success", success_message): Displays a success message if the command completes successfully.
    • messagebox.showerror("Error", f"Command failed with return code {result.returncode}"): Displays an error message if the command fails.
  4. Non-Windows Handling:

    • Executes the command normally if the operating system is not Windows.
  5. Admin Elevation:

    • elevate(): Restarts the script with administrative privileges if it is not already running as an admin.
    • root.quit() and os._exit(0): Closes the current window and exits the process to restart the script with elevated rights.
  6. Exception Handling:

    • log_error(): Logs errors if exceptions occur during command execution.
    • messagebox.showerror("Error", f"An error occurred: {str(e)}"): Displays an error message if an exception is caught.

Function: run_window_admin_command(command, success_message)

def run_window_admin_command(command, success_message):
    try:
        if platform.system() == "Windows":
            if is_admin():
                # Execute command in a new CMD window
                full_command = f'start cmd /k "{command}"'
                result = subprocess.run(full_command, shell=True)
                if result.returncode == 0 or result.returncode == 3:  # Ignore exit code 3
                    messagebox.showinfo("Success", success_message)
                else:
                    messagebox.showerror("Error", f"Command failed with return code {result.returncode}")
            else:
                # Restart script as administrator
                messagebox.showinfo("Info", "This action requires administrator privileges. The program will restart with elevated rights.")
                elevate()
                root.quit()  # Close the old window
                os._exit(0)  # Exit the old window immediately
        else:
            # For other operating systems: execute command normally
            result = subprocess.run(command, shell=True)
            if result.returncode == 0 or result.returncode == 3:  # Ignore exit code 3
                messagebox.showinfo("Success", success_message)
            else:
                messagebox.showerror("Error", f"Command failed with return code {result.returncode}")
    except Exception as e:
        # Log error and display message
        log_error(f"Error running admin command: {command}", e)
        messagebox.showerror("Error", f"An error occurred: {str(e)}")

Purpose:
The run_window_admin_command() function is similar to run_admin_command() but specifically runs the command in a new command prompt (CMD) window on Windows. This is useful when the command output or interaction is needed in a visible CMD window.

Detailed Explanation:

  1. Admin Check and Command Execution:

    • full_command = f'start cmd /k "{command}"':
      • start cmd /k: Opens a new CMD window and executes the command.
      • /k: Tells CMD to run the command and then remain open, allowing the user to see the output.
  2. Other Similarities:

    • Handles both Windows and non-Windows platforms.
    • Elevates privileges if necessary.
    • Provides feedback and error handling similar to run_admin_command().

Summary

The run_admin_command() and run_window_admin_command() functions are utilities for executing system commands with administrative privileges on Windows. They handle command execution both with and without a visible CMD window and include mechanisms for checking platform compatibility, handling administrative rights, and providing user feedback and error handling. The run_window_admin_command() function specifically opens a new CMD window to show command output, while run_admin_command() can optionally hide the command window. Both functions ensure the script is running with appropriate privileges and handle errors gracefully.


Code Explanation for Checking Internet Speed

This section of the code defines a function to measure the internet speed using the Speedtest library. It calculates the download and upload speeds, as well as the ping, and handles potential errors that may occur during the speed test.

Function: get_internet_speed()

def get_internet_speed():
    try:
        st = Speedtest()
        st.get_best_server()
        download_speed = st.download() / 1_000_000
        upload_speed = st.upload() / 1_000_000
        ping = st.results.ping
        return download_speed, upload_speed, ping
    except ConfigRetrievalError as e:
        log_error("Failed to retrieve speedtest configuration", e)
        messagebox.showerror("Speedtest Error", "Failed to retrieve speedtest configuration. Please check the log file for details.")
        return 0, 0, 0

Purpose:
The get_internet_speed() function measures and returns the current internet speed, including download speed, upload speed, and ping. This function utilizes the Speedtest library to perform these measurements.

Detailed Explanation:

  1. Initialize Speedtest:

    • st = Speedtest():
      • Creates an instance of the Speedtest class from the speedtest library. This class is used to interact with the Speedtest service to measure internet speed.
  2. Get Best Server:

    • st.get_best_server():
      • Finds the best server based on latency (ping) for performing the speed test. This helps in selecting a server that provides the most accurate speed measurement for the user's location.
  3. Measure Speeds:

    • download_speed = st.download() / 1_000_000:
      • Measures the download speed in bits per second (bps) and converts it to megabits per second (Mbps) by dividing by 1,000,000. This conversion is often used for readability and comparison.
    • upload_speed = st.upload() / 1_000_000:
      • Measures the upload speed in bits per second (bps) and converts it to megabits per second (Mbps).
    • ping = st.results.ping:
      • Retrieves the ping (latency) in milliseconds (ms), which indicates the time it takes for data to travel from the client to the server and back.
  4. Return Values:

    • return download_speed, upload_speed, ping:
      • Returns the measured download speed, upload speed, and ping.
  5. Error Handling:

    • except ConfigRetrievalError as e::
      • Catches exceptions related to configuration retrieval failures from the Speedtest library. This can occur if the library fails to fetch server or configuration details.
    • log_error("Failed to retrieve speedtest configuration", e):
      • Logs the error using a logging function (assumed to be defined elsewhere in the code). This is useful for debugging and tracking issues related to the Speedtest configuration.
    • messagebox.showerror("Speedtest Error", "Failed to retrieve speedtest configuration. Please check the log file for details."):
      • Displays an error message box to inform the user that there was an issue with retrieving the Speedtest configuration and directs them to check the log file for more details.
    • return 0, 0, 0:
      • Returns default values (0 for download speed, upload speed, and ping) in case of an error, ensuring that the function still returns a valid tuple even if the speed test fails.

Usage Example

This function can be used to get the current internet speed and display it or use it for further processing.

# Example usage
download_speed, upload_speed, ping = get_internet_speed()
print(f"Download Speed: {download_speed} Mbps")
print(f"Upload Speed: {upload_speed} Mbps")
print(f"Ping: {ping} ms")

Summary

The get_internet_speed() function measures and returns the download speed, upload speed, and ping of the user's internet connection using the Speedtest-cli library. It handles exceptions that may arise during the configuration retrieval phase and provides user feedback through a message box if an error occurs. This function is essential for applications that need to report or analyze internet performance metrics.


Code Explanation for Retrieving PC Information

This section of the code defines a function that gathers and formats various pieces of information about the PC, including CPU details, memory usage, disk usage, operating system, network details, and system uptime.

Function: get_pc_info()

def get_pc_info():
    cpu_info = f"CPU: {psutil.cpu_count(logical=False)} cores ({psutil.cpu_count()} logical processors)\n"
    memory_info = f"Memory: {psutil.virtual_memory().total / (1024 ** 3):.2f} GB\n"
    available_memory_info = f"Available Memory: {psutil.virtual_memory().available / (1024 ** 3):.2f} GB\n"
    disk_info = f"Disk Usage: {psutil.disk_usage('/').total / (1024 ** 3):.2f} GB total\n"
    os_info = f"OS: {platform.system()} {platform.release()}\n"
    hostname = socket.gethostname()
    ip_address = socket.gethostbyname(hostname)
    network_info = f"Hostname: {hostname}\nIP Address: {ip_address}\n"
    boot_time = psutil.boot_time()
    uptime = time.time() - boot_time
    uptime_info = f"Uptime: {int(uptime // 3600)} hours, {int((uptime % 3600) // 60)} minutes\n"
    
    info = cpu_info + memory_info + available_memory_info + disk_info + os_info + network_info + uptime_info
    return info

Purpose:
The get_pc_info() function collects and formats information about the PC's hardware and software environment. This includes details about the CPU, memory, disk usage, operating system, network configuration, and system uptime. The formatted information is returned as a single string.

Detailed Explanation:

  1. CPU Information:

    • psutil.cpu_count(logical=False):
      • Returns the number of physical CPU cores.
    • psutil.cpu_count():
      • Returns the total number of logical (virtual) processors, which includes hyper-threaded cores if applicable.
    • cpu_info:
      • Formats and combines the number of physical and logical processors into a readable string.
  2. Memory Information:

    • psutil.virtual_memory().total:
      • Retrieves the total physical memory (RAM) in bytes.
    • psutil.virtual_memory().available:
      • Retrieves the available physical memory in bytes.
    • memory_info and available_memory_info:
      • Convert bytes to gigabytes (GB) and format the total and available memory into strings.
  3. Disk Usage:

    • psutil.disk_usage('/'):
      • Provides disk usage statistics for the root directory ('/').
    • disk_info:
      • Formats the total disk space in gigabytes (GB) into a string.
  4. Operating System Information:

    • platform.system():
      • Returns the name of the operating system (e.g., 'Windows', 'Linux').
    • platform.release():
      • Returns the version of the operating system.
    • os_info:
      • Formats the operating system name and version into a string.
  5. Network Information:

    • socket.gethostname():
      • Retrieves the hostname of the PC.
    • socket.gethostbyname(hostname):
      • Resolves the IP address associated with the hostname.
    • network_info:
      • Formats the hostname and IP address into a string.
  6. Uptime Information:

    • psutil.boot_time():
      • Returns the system boot time as a timestamp.
    • time.time():
      • Gets the current time as a timestamp.
    • uptime:
      • Calculates the system uptime in seconds by subtracting the boot time from the current time.
    • uptime_info:
      • Formats the uptime into hours and minutes.
  7. Combine Information:

    • info:
      • Concatenates all the formatted strings into a single string that represents the complete PC information.
  8. Return Information:

    • return info:
      • Returns the concatenated string containing all the gathered information about the PC.

Usage Example

This function can be used to retrieve and display or log comprehensive information about the PC's hardware and software environment.

# Example usage
pc_info = get_pc_info()
print(pc_info)

Summary

The get_pc_info() function provides a comprehensive overview of the PC's hardware and software environment by gathering details about the CPU, memory, disk usage, operating system, network configuration, and system uptime. It formats this information into a readable string and returns it, which can be used for display, logging, or further processing.


Code Explanation for Retrieving Network Information

This section of the code defines two functions for gathering network-related information. The first function retrieves the network name, and the second function provides comprehensive connection information, including IP address, network name, signal strength, and provider details.

Function: get_network_name()

def get_network_name():
    try:
        result = subprocess.run(['powershell', '-Command', 'Get-NetConnectionProfile | Select-Object -ExpandProperty Name'], capture_output=True, text=True)
        network_name = result.stdout.strip()
        return network_name if network_name else "Not connected"
    except Exception as e:
        log_error("Failed to retrieve network name", e)
        return "Not connected"

Purpose:
The get_network_name() function retrieves the name of the current network connection on a Windows system using PowerShell. If the network name cannot be obtained or if an error occurs, it returns "Not connected".

Detailed Explanation:

  1. Run PowerShell Command:

    • subprocess.run():
      • Executes a PowerShell command to get the network connection profile name.
      • ['powershell', '-Command', 'Get-NetConnectionProfile | Select-Object -ExpandProperty Name']:
        • Runs PowerShell with the Get-NetConnectionProfile cmdlet to fetch the network connection profile and extracts the name property.
      • capture_output=True: Captures the output of the command.
      • text=True: Ensures the output is returned as a string rather than bytes.
  2. Process Result:

    • result.stdout.strip():
      • Retrieves the standard output of the command and removes any leading or trailing whitespace.
    • return network_name if network_name else "Not connected":
      • Returns the network name if available. If the output is empty, it returns "Not connected".
  3. Error Handling:

    • except Exception as e::
      • Catches any exceptions that occur during the command execution.
    • log_error("Failed to retrieve network name", e):
      • Logs the error for debugging purposes.
    • return "Not connected":
      • Returns "Not connected" if an exception is caught.

Function: get_connection_info()

def get_connection_info():
    try:
        ip_address = socket.gethostbyname(socket.gethostname())
        network_name = get_network_name()
        signal_strength = "Unknown"  # Signal strength is not easily retrievable in Windows
        provider = "Unknown"  # Provider is not easily retrievable without third-party tools
        return f"IP Address: {ip_address}\nNetwork Name: {network_name}\nSignal Strength: {signal_strength}\nProvider: {provider}"
    except Exception as e:
        log_error("Failed to retrieve connection info", e)
        return "Failed to retrieve connection information."

Purpose:
The get_connection_info() function gathers and formats information about the current network connection, including the IP address, network name, signal strength, and provider. It returns this information as a formatted string.

Detailed Explanation:

  1. Retrieve IP Address:

    • socket.gethostbyname(socket.gethostname()):
      • Retrieves the IP address associated with the local hostname. This provides the local IP address of the machine.
  2. Get Network Name:

    • network_name = get_network_name():
      • Calls the get_network_name() function to obtain the name of the current network connection.
  3. Signal Strength and Provider:

    • signal_strength = "Unknown":
      • Sets a placeholder value for signal strength, as this information is not easily accessible on Windows without third-party tools.
    • provider = "Unknown":
      • Sets a placeholder value for the network provider, as this information is also not easily accessible without third-party tools.
  4. Format and Return Information:

    • return f"IP Address: {ip_address}\nNetwork Name: {network_name}\nSignal Strength: {signal_strength}\nProvider: {provider}":
      • Formats the collected information into a string and returns it.
  5. Error Handling:

    • except Exception as e::
      • Catches any exceptions that occur during the retrieval of connection information.
    • log_error("Failed to retrieve connection info", e):
      • Logs the error for debugging purposes.
    • return "Failed to retrieve connection information.":
      • Returns an error message if an exception is caught.

Summary

The get_network_name() function retrieves the network name of the current connection using a PowerShell command and handles errors gracefully. The get_connection_info() function aggregates various network-related details, including IP address, network name, and placeholders for signal strength and provider information. It formats and returns this data as a readable string. Both functions include error handling to ensure that they return appropriate messages if an issue occurs.


Code Explanation for Displaying Speedtest Results

This section of the code defines a function that displays the results of an internet speed test in a new window using the Tkinter library. It shows the download speed, upload speed, and ping values in a formatted text widget.

Function: show_speedtest_result()

def show_speedtest_result():
    download_speed, upload_speed, ping = get_internet_speed()
    result = f"Download Speed: {download_speed:.2f} Mbps\nUpload Speed: {upload_speed:.2f} Mbps\nPing: {ping} ms"
    info_window = tk.Toplevel(root)
    info_window.title("Speedtest Result")
    info_window.geometry("400x200")
    text = tk.Text(info_window)
    text.insert(tk.END, result)
    text.config(state=tk.DISABLED)
    text.pack(expand=True, fill=tk.BOTH, padx=10, pady=10)

Purpose:
The show_speedtest_result() function performs an internet speed test, formats the results, and displays them in a new Tkinter window. This function provides a user-friendly way to view the speed test results.

Detailed Explanation:

  1. Retrieve Speedtest Results:

    • download_speed, upload_speed, ping = get_internet_speed():
      • Calls the get_internet_speed() function to get the current internet speed statistics: download speed, upload speed, and ping.
  2. Format the Result:

    • result = f"Download Speed: {download_speed:.2f} Mbps\nUpload Speed: {upload_speed:.2f} Mbps\nPing: {ping} ms":
      • Formats the speed test results into a string. The download and upload speeds are displayed with two decimal places, and the ping is shown as-is.
  3. Create a New Tkinter Window:

    • info_window = tk.Toplevel(root):
      • Creates a new top-level window (child window) that is independent of the main application window (root).
    • info_window.title("Speedtest Result"):
      • Sets the title of the new window to "Speedtest Result".
    • info_window.geometry("400x200"):
      • Sets the size of the new window to 400x200 pixels.
  4. Create and Configure Text Widget:

    • text = tk.Text(info_window):
      • Creates a Text widget within the new window to display multi-line text.
    • text.insert(tk.END, result):
      • Inserts the formatted speed test result string into the Text widget.
    • text.config(state=tk.DISABLED):
      • Disables editing of the Text widget to make it read-only.
    • text.pack(expand=True, fill=tk.BOTH, padx=10, pady=10):
      • Packs the Text widget into the window with expansion and padding settings to ensure it fills the available space and is visually pleasing.

Summary

The show_speedtest_result() function performs an internet speed test and displays the results in a new Tkinter window. It retrieves the download speed, upload speed, and ping using the get_internet_speed() function, formats these results into a readable string, and then presents this information in a new, non-editable text widget within a pop-up window. This provides users with an intuitive interface to view their internet speed test results.


Code Explanation for Displaying Connection Information

This section of the code defines a function to display network connection information in a new Tkinter window. The information includes IP address, network name, signal strength, and provider details.

Function: show_connection_info()

def show_connection_info():
    info = get_connection_info()
    info_window = tk.Toplevel(root)
    info_window.title("Connection Information")
    info_window.geometry("400x300")
    text = tk.Text(info_window)
    text.insert(tk.END, info)
    text.config(state=tk.DISABLED)
    text.pack(expand=True, fill=tk.BOTH, padx=10, pady=10)

Purpose:
The show_connection_info() function retrieves network connection details and displays them in a new window using Tkinter. This provides users with a clear and organized view of their network information.

Detailed Explanation:

  1. Retrieve Connection Information:

    • info = get_connection_info():
      • Calls the get_connection_info() function to fetch the network-related information. This includes the IP address, network name, signal strength, and provider.
  2. Create a New Tkinter Window:

    • info_window = tk.Toplevel(root):
      • Creates a new top-level window (a child window) that is separate from the main application window (root).
    • info_window.title("Connection Information"):
      • Sets the title of the new window to "Connection Information".
    • info_window.geometry("400x300"):
      • Defines the dimensions of the new window as 400x300 pixels.
  3. Create and Configure Text Widget:

    • text = tk.Text(info_window):
      • Initializes a Text widget within the new window for displaying multiple lines of text.
    • text.insert(tk.END, info):
      • Inserts the retrieved connection information into the Text widget.
    • text.config(state=tk.DISABLED):
      • Configures the Text widget to be read-only by setting its state to DISABLED. This prevents user modifications.
    • text.pack(expand=True, fill=tk.BOTH, padx=10, pady=10):
      • Packs the Text widget into the window with settings to expand it and fill available space. It also adds padding around the widget for better layout.

Summary

The show_connection_info() function gathers network connection details and displays them in a new Tkinter window. It retrieves the information using the get_connection_info() function, formats it into a string, and shows it in a non-editable Text widget within a pop-up window. This function provides an organized and user-friendly way to view detailed network information.


Code Explanation for Displaying PC Information

This section of the code defines a function to display information about the PC in a message box using the Tkinter library. The information includes details about the CPU, memory, disk usage, operating system, network configuration, and system uptime.

Function: show_pc_info()

def show_pc_info():
    info = get_pc_info()
    messagebox.showinfo("PC Information", info)

Purpose:
The show_pc_info() function retrieves and displays comprehensive information about the PC in a message box. This provides a quick and user-friendly way to view detailed system information.

Detailed Explanation:

  1. Retrieve PC Information:

    • info = get_pc_info():
      • Calls the get_pc_info() function to obtain various details about the PC. This function gathers information about the CPU, memory, disk usage, operating system, network configuration, and system uptime.
  2. Display Information in a Message Box:

    • messagebox.showinfo("PC Information", info):
      • Uses Tkinter's messagebox.showinfo() function to display the retrieved information.
      • "PC Information":
        • Sets the title of the message box to "PC Information".
      • info:
        • Passes the gathered information as the message content to be displayed in the message box.

Summary

The show_pc_info() function provides a straightforward way to view detailed PC information. It retrieves the information using the get_pc_info() function and displays it in a Tkinter message box. This approach allows users to quickly access and review their system's hardware and software details in a concise and readable format.


Code Explanation for Running a Command

This section of the code defines a function to execute a system command and handle its output. The function captures the command’s output, logs it, and displays a success or error message based on the execution result.

Function: run_command(command, success_message)

def run_command(command, success_message):
    try:
        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        stdout, stderr = process.communicate()
        
        # Detect encoding
        result_encoding = chardet.detect(stdout)['encoding']
        if result_encoding is None:
            result_encoding = 'utf-8'  # Fallback to default encoding
        
        if process.returncode == 0:
            log_command(command, stdout.decode(result_encoding, errors='ignore'))
            messagebox.showinfo("Success", success_message)
        else:
            log_command(command, stderr.decode(result_encoding, errors='ignore'))
            raise subprocess.CalledProcessError(process.returncode, command, output=stderr)
    except Exception as e:
        log_error(f"Error running command: {command}", e)
        messagebox.showerror("Error", "An error occurred. Please check the log file for details.")

Purpose:
The run_command() function executes a specified system command, captures its output, and displays a success or error message based on the command's execution result. It also logs the command's output or error message for further inspection.

Detailed Explanation:

  1. Execute the Command:

    • subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True):
      • Runs the specified command in a subprocess.
      • stdout=subprocess.PIPE and stderr=subprocess.PIPE:
        • Redirects the standard output and error streams to be captured.
      • shell=True:
        • Executes the command through the shell.
  2. Capture Output and Error:

    • stdout, stderr = process.communicate():
      • Retrieves the output and error streams from the subprocess after it completes execution.
  3. Detect and Handle Encoding:

    • result_encoding = chardet.detect(stdout)['encoding']:
      • Uses the chardet library to detect the encoding of the command’s output.
    • if result_encoding is None::
      • Checks if the detected encoding is None.
    • result_encoding = 'utf-8':
      • Falls back to 'utf-8' if encoding detection fails.
  4. Check Command Execution Result:

    • if process.returncode == 0::
      • Checks if the command completed successfully (return code 0).
    • log_command(command, stdout.decode(result_encoding, errors='ignore')):
      • Logs the successful output of the command.
    • messagebox.showinfo("Success", success_message):
      • Displays a success message to the user.
  5. Handle Errors:

    • else::
      • Handles the case where the command fails (non-zero return code).
    • log_command(command, stderr.decode(result_encoding, errors='ignore')):
      • Logs the error output from the command.
    • raise subprocess.CalledProcessError(process.returncode, command, output=stderr):
      • Raises an exception to signal that the command failed.
  6. Exception Handling:

    • except Exception as e::
      • Catches any exceptions that occur during command execution.
    • log_error(f"Error running command: {command}", e):
      • Logs the exception for debugging purposes.
    • messagebox.showerror("Error", "An error occurred. Please check the log file for details."):
      • Displays an error message to the user, instructing them to check the log file for more details.

Summary

The run_command() function executes a system command and manages its output and errors. It captures and decodes the command's output, logs the results, and provides feedback to the user through message boxes. This function is robust, handling both successful and failed executions while ensuring that any issues are logged and reported appropriately.


Code Explanation for Logging Commands and Errors

This section of the code defines two functions for logging command executions and errors to a log file. These functions help keep track of commands run and any issues encountered during execution, providing a record for debugging and review.

Function: log_command(command, output)

def log_command(command, output):
    with open(os.path.join(script_dir, "log.txt"), "a") as f:
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        f.write(f"[COMMAND] [{timestamp}] {command}\n")
        f.write(f"[OUTPUT] {output}\n")

Purpose:
The log_command() function appends details about a system command execution to a log file. This includes the command itself and its output.

Detailed Explanation:

  1. Open the Log File:

    • with open(os.path.join(script_dir, "log.txt"), "a") as f::
      • Opens the log file (log.txt) in append mode ("a"). The log file is located in the directory where the script is executed (script_dir).
  2. Get Current Timestamp:

    • timestamp = time.strftime("%Y-%m-%d %H:%M:%S"):
      • Retrieves the current time formatted as YYYY-MM-DD HH:MM:SS. This timestamp is used to log when the command was executed.
  3. Write Command and Output to Log:

    • f.write(f"[COMMAND] [{timestamp}] {command}\n"):
      • Writes the command executed along with the timestamp to the log file.
    • f.write(f"[OUTPUT] {output}\n"):
      • Writes the output of the command to the log file.

Function: log_error(message, error)

def log_error(message, error):
    with open(os.path.join(script_dir, "log.txt"), "a") as f:
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        f.write(f"[ERROR] [{timestamp}] {message}\n")
        f.write(f"[DETAILS] {str(error)}\n")

Purpose:
The log_error() function appends details about an error to the log file. This includes a descriptive message about the error and the error details.

Detailed Explanation:

  1. Open the Log File:

    • with open(os.path.join(script_dir, "log.txt"), "a") as f::
      • Opens the log file (log.txt) in append mode ("a"), similar to the log_command() function.
  2. Get Current Timestamp:

    • timestamp = time.strftime("%Y-%m-%d %H:%M:%S"):
      • Retrieves the current time in YYYY-MM-DD HH:MM:SS format to timestamp the log entry.
  3. Write Error Information to Log:

    • f.write(f"[ERROR] [{timestamp}] {message}\n"):
      • Logs the error message along with the timestamp.
    • f.write(f"[DETAILS] {str(error)}\n"):
      • Logs detailed information about the error, including the exception message converted to a string.

Summary

The log_command() and log_error() functions provide mechanisms for recording command executions and errors to a log file. The log_command() function records the commands run and their outputs, while the log_error() function captures error messages and detailed exception information. Both functions use timestamps to annotate the logs, facilitating troubleshooting and maintaining a clear record of events.


Code Explanation for Displaying Menus

This section of the code defines functions for displaying different menus in a Tkinter application. Each function clears the current frame and then displays a specific set of buttons corresponding to different functionalities (e.g., system information, cleaning tasks, updates, and repairs).

Function: show_info_menu()

def show_info_menu():
    clear_frame()
    systeminfo_button.pack(pady=10)
    advanced_systeminfo_button.pack(pady=10)
    resource_monitoring_button.pack(pady=10)
    speedtest_button.pack(pady=10)
    connection_button.pack(pady=10)
    back_button.pack(pady=10)

Purpose:
Displays the menu for viewing system information, which includes buttons for various system information tasks.

Detailed Explanation:

  1. Clear Current Frame:

    • clear_frame():
      • Clears the current frame of any existing widgets or content to prepare for the new menu.
  2. Display Buttons:

    • systeminfo_button.pack(pady=10):
      • Adds the system information button to the frame with padding.
    • advanced_systeminfo_button.pack(pady=10):
      • Adds the advanced system information button.
    • resource_monitoring_button.pack(pady=10):
      • Adds the resource monitoring button.
    • speedtest_button.pack(pady=10):
      • Adds the speed test button.
    • connection_button.pack(pady=10):
      • Adds the connection information button.
    • back_button.pack(pady=10):
      • Adds the back button for navigating to the previous menu.

Function: show_clean_menu()

def show_clean_menu():
    clear_frame()
    rm_bloatware_button.pack(pady=10)
    clean_mngr_button.pack(pady=10)
    wsreset_button.pack(pady=10)
    disk_cleanup_button.pack(pady=10)
    temp_cleanup_button.pack(pady=10)
    prefetch_clean_button.pack(pady=10)
    clean_invis_button.pack(pady=10)
    defragment_button.pack(pady=10)
    clean_vs_button.pack(pady=10)
    Empty_RecycleBin_button.pack(pady=10)
    back_button.pack(pady=10)

Purpose:
Displays the menu for cleaning tasks, with buttons for various cleaning functions.

Detailed Explanation:

  1. Clear Current Frame:

    • clear_frame():
      • Clears the current frame to prepare for the new set of cleaning-related buttons.
  2. Display Buttons:

    • rm_bloatware_button.pack(pady=10):
      • Adds the remove bloatware button.
    • clean_mngr_button.pack(pady=10):
      • Adds the clean manager button.
    • wsreset_button.pack(pady=10):
      • Adds the Windows Store reset button.
    • disk_cleanup_button.pack(pady=10):
      • Adds the disk cleanup button.
    • temp_cleanup_button.pack(pady=10):
      • Adds the temporary files cleanup button.
    • prefetch_clean_button.pack(pady=10):
      • Adds the prefetch cleanup button.
    • clean_invis_button.pack(pady=10):
      • Adds the clean invisible files button.
    • defragment_button.pack(pady=10):
      • Adds the defragmentation button.
    • clean_vs_button.pack(pady=10):
      • Adds the virtual storage cleanup button.
    • Empty_RecycleBin_button.pack(pady=10):
      • Adds the empty recycle bin button.
    • back_button.pack(pady=10):
      • Adds the back button for navigation.

Function: show_update_menu()

def show_update_menu():
    clear_frame()
    update_apps_button.pack(pady=10)
    windows_update_button.pack(pady=10)
    driver_update_button.pack(pady=10)
    back_button.pack(pady=10)

Purpose:
Displays the menu for update tasks, including buttons for updating applications, Windows, and drivers.

Detailed Explanation:

  1. Clear Current Frame:

    • clear_frame():
      • Clears the current frame to make space for the update-related buttons.
  2. Display Buttons:

    • update_apps_button.pack(pady=10):
      • Adds the update applications button.
    • windows_update_button.pack(pady=10):
      • Adds the Windows update button.
    • driver_update_button.pack(pady=10):
      • Adds the driver update button.
    • back_button.pack(pady=10):
      • Adds the back button for navigating to the previous menu.

Function: show_repair_menu()

def show_repair_menu():
    clear_frame()
    health_scan_button.pack(pady=10)
    storage_diagonistics_button.pack(pady=10)
    repair_file_system_button.pack(pady=10)
    repair_connection_button.pack(pady=10)
    back_button.pack(pady=10)

Purpose:
Displays the menu for repair tasks, featuring buttons for various system repair functions.

Detailed Explanation:

  1. Clear Current Frame:

    • clear_frame():
      • Clears the current frame to set up for the repair-related buttons.
  2. Display Buttons:

    • health_scan_button.pack(pady=10):
      • Adds the health scan button.
    • storage_diagonistics_button.pack(pady=10):
      • Adds the storage diagnostics button.
    • repair_file_system_button.pack(pady=10):
      • Adds the repair file system button.
    • repair_connection_button.pack(pady=10):
      • Adds the repair connection button.
    • back_button.pack(pady=10):
      • Adds the back button for navigation.

Function: show_main_menu()

def show_main_menu():
    clear_frame()
    info_button.pack(pady=10)
    clean_button.pack(pady=10)
    update_button.pack(pady=10)
    repair_button.pack(pady=10)
    help_button.pack(pady=10)

Purpose:
Displays the main menu with buttons for accessing the different functionality menus.

Detailed Explanation:

  1. Clear Current Frame:

    • clear_frame():
      • Clears the current frame to prepare for the main menu buttons.
  2. Display Buttons:

    • info_button.pack(pady=10):
      • Adds the button for accessing the information menu.
    • clean_button.pack(pady=10):
      • Adds the button for accessing the cleaning menu.
    • update_button.pack(pady=10):
      • Adds the button for accessing the update menu.
    • repair_button.pack(pady=10):
      • Adds the button for accessing the repair menu.
    • help_button.pack(pady=10):
      • Adds the button for accessing the help section.

Summary

The functions show_info_menu(), show_clean_menu(), show_update_menu(), show_repair_menu(), and show_main_menu() are responsible for managing the display of different sets of buttons in a Tkinter application. Each function clears the current frame using clear_frame() and then packs the appropriate buttons for the selected menu. This modular approach allows for dynamic and organized navigation within the application.


Code Explanation for Clearing Widgets

The clear_frame() function is used to remove all widgets from a Tkinter frame. This is particularly useful for updating the user interface by hiding or removing existing widgets before displaying new ones.

Function: clear_frame()

def clear_frame():
    for widget in root.winfo_children():
        widget.pack_forget()

Purpose:
The clear_frame() function hides all widgets in the current frame by calling pack_forget() on each widget. This effectively clears the frame, allowing for a fresh display of new widgets.

Detailed Explanation:

  1. Iterate Over All Widgets:

    • for widget in root.winfo_children()::
      • root.winfo_children() returns a list of all child widgets currently contained in the root window (or frame). The for loop iterates over each widget in this list.
  2. Remove Widgets from View:

    • widget.pack_forget():
      • The pack_forget() method is called on each widget. This method removes the widget from the view without destroying it. The widget's configuration and state are preserved, but it is no longer visible or part of the layout.

Use Case:

  • Dynamic UI Updates: When switching between different sections or menus in a Tkinter application, clear_frame() can be used to hide all widgets from the current view before displaying a new set of widgets. This ensures that the UI remains organized and only shows the relevant widgets for the current context.
  • Navigation: In applications where different menus or settings are presented, clear_frame() helps in transitioning smoothly between these different views without leaving residual widgets from the previous view.

Summary:
The clear_frame() function provides a way to clean up the Tkinter frame by hiding all current widgets, enabling a seamless update of the user interface. This function is useful for managing dynamic content and ensuring that the frame is ready for new widgets to be displayed.


Code Explanation for Downloading and Extracting a ZIP Archive

The download_and_extract_zip function handles the process of downloading a ZIP file from a specified URL, extracting its contents to a target directory, and then executing a batch file from the extracted contents. This function is useful for automating the retrieval and execution of scripts or utilities packaged in ZIP files.

Function: download_and_extract_zip(url, extract_to='. ')

def download_and_extract_zip(url, extract_to='.'):
    # Create the target directory path for the 'debloat' folder
    debloat_folder = os.path.join(extract_to, 'debloat')
    
    # Check if the 'debloat' folder exists, and if not, create it
    if not os.path.exists(debloat_folder):
        os.makedirs(debloat_folder)

    # The name of the file to be downloaded
    local_filename = os.path.join(debloat_folder, 'debloat.zip')
    
    try:
        # Download the file
        with requests.get(url, stream=True) as r:
            r.raise_for_status()
            with open(local_filename, 'wb') as f:
                for chunk in r.iter_content(chunk_size=8192):
                    f.write(chunk)
        
        # Extract the ZIP file into the 'debloat' folder
        with zipfile.ZipFile(local_filename, 'r') as zip_ref:
            zip_ref.extractall(debloat_folder)
        
        # Remove the ZIP archive after extraction
        os.remove(local_filename)
        
        # Execute the Run.bat file in the 'debloat' folder
        run_bat_path = os.path.join(debloat_folder, 'Run.bat')
        subprocess.run(f'"{run_bat_path}"', shell=True)
        
        messagebox.showinfo("Success", "The script has been downloaded and executed successfully!")
    except Exception as e:
        messagebox.showerror("Error", f"An error occurred: {e}")

Purpose:
The function is designed to automate the process of downloading a ZIP file, extracting its contents, and running a specific batch file contained within the ZIP file. This is useful for scenarios where a script or tool is distributed as a ZIP archive and needs to be set up and executed on a local system.

Detailed Explanation:

  1. Create Target Directory:

    • debloat_folder = os.path.join(extract_to, 'debloat'):
      • Constructs the path for the directory where the ZIP file will be extracted.
    • if not os.path.exists(debloat_folder): os.makedirs(debloat_folder):
      • Checks if the debloat folder exists. If it does not, it creates the folder to ensure the ZIP file has a proper location for extraction.
  2. Define Local Filename:

    • local_filename = os.path.join(debloat_folder, 'debloat.zip'):
      • Defines the path where the downloaded ZIP file will be saved locally.
  3. Download the ZIP File:

    • with requests.get(url, stream=True) as r::
      • Sends a GET request to download the ZIP file in a streaming fashion to handle large files efficiently.
    • r.raise_for_status():
      • Checks if the request was successful (status code 200). Raises an exception for HTTP errors.
    • with open(local_filename, 'wb') as f::
      • Opens a local file to write the downloaded content in binary mode.
    • for chunk in r.iter_content(chunk_size=8192): f.write(chunk):
      • Writes the downloaded content in chunks to avoid high memory usage.
  4. Extract the ZIP File:

    • with zipfile.ZipFile(local_filename, 'r') as zip_ref::
      • Opens the downloaded ZIP file for reading.
    • zip_ref.extractall(debloat_folder):
      • Extracts all contents of the ZIP file into the debloat folder.
  5. Remove ZIP Archive:

    • os.remove(local_filename):
      • Deletes the ZIP file after successful extraction to clean up space.
  6. Execute Batch File:

    • run_bat_path = os.path.join(debloat_folder, 'Run.bat'):
      • Constructs the path for the Run.bat file located in the extracted folder.
    • subprocess.run(f'"{run_bat_path}"', shell=True):
      • Executes the batch file using the command line. The shell=True argument allows the command to be executed in the shell.
  7. Show Result Messages:

    • messagebox.showinfo("Success", "The script has been downloaded and executed successfully!"):
      • Displays a success message when the process completes without errors.
    • messagebox.showerror("Error", f"An error occurred: {e}"):
      • Displays an error message if an exception occurs during any step.

Use Case:

  • Automation: This function is ideal for automating the deployment of tools or scripts packaged as ZIP files. It ensures that the setup process involves downloading, extracting, and running the necessary files with minimal user intervention.
  • Maintenance Scripts: Useful in scenarios where maintenance or cleanup scripts need to be regularly updated and executed from a centralized source.

Summary:
The download_and_extract_zip function automates the process of downloading a ZIP file from a given URL, extracting its contents to a specific folder, and executing a batch file within the extracted contents. It provides a streamlined way to manage and deploy tools or scripts distributed in ZIP format.


Code Explanation for Handling Advanced Debloat Button

The advanced_debloat function is designed to handle the event when the user clicks the "Advanced Debloat" button in a Tkinter application. This function prompts the user with a message about the implications of running an advanced debloating script and, based on the user's response, downloads and executes the script.

Function: advanced_debloat()

def advanced_debloat():
    response = messagebox.askyesno(
        "Advanced Debloat",
        ("This will download a script powered by Raphire that provides advanced debloating functions for your PC, "
         "including the ability to disable Microsoft tracking. "
         "You can run the script later by going to the debloat folder and executing the Run.bat file, "
         "which also allows you to undo any changes. However, please be careful and do not modify the code if you "
         "are not familiar with it, as this could render the system unusable. I assume no liability. "
         "Do you want to download and run the script?")
    )
    
    if response:
        # If the user clicks Yes
        download_link = "https://cdn.discordapp.com/attachments/1276569836947902605/1276945729407025233/HBGs7VT.zip?ex=66cb5fe2&is=66ca0e62&hm=0ef399438ef74a887ccfadc6a9a1898f23c0d8969ec386ee7b889ab26e1afa89&"  # Discord link here
        download_and_extract_zip(download_link, os.getcwd())
    else:
        # If the user clicks No, simply close the popup
        pass

Purpose:
The function provides a user interface prompt to confirm the user's intention to download and run an advanced debloating script. It ensures the user is aware of the script's functionality and potential risks before proceeding.

Detailed Explanation:

  1. Prompt User for Confirmation:

    • messagebox.askyesno("Advanced Debloat", ...):
      • Displays a confirmation dialog box with a yes/no question. The message explains what the script will do, including potential risks, and warns against modifying the script if the user is unfamiliar with its content.
      • The dialog box title is "Advanced Debloat".
      • The message details the functionality of the script, including debloating features and the ability to undo changes via the Run.bat file.
  2. Handle User Response:

    • if response::
      • Checks if the user clicked "Yes". If true, the function proceeds with downloading and extracting the ZIP file. If false, it does nothing and the dialog box closes.
  3. Download and Extract the Script:

    • download_link = "...":
      • Defines the URL where the ZIP file can be downloaded. This URL points to a file hosted on Discord.
    • download_and_extract_zip(download_link, os.getcwd()):
      • Calls the download_and_extract_zip function to download the ZIP file from the specified URL and extract it to the current working directory (os.getcwd()).
  4. Handle User Choosing "No":

    • else: pass:
      • If the user clicks "No", the function does nothing further and the popup simply closes. The pass statement is used to explicitly do nothing in this case.

Use Case:

  • Advanced Tool Deployment: This function is used in scenarios where advanced tools or scripts need to be deployed with user consent. It is especially relevant when the script involves significant changes to the system configuration or settings.
  • User Warnings: Ensures users are adequately warned about the potential impact of running the script, promoting safer execution and reducing the risk of unintended system issues.

Summary:
The advanced_debloat function prompts users with a warning about the implications of running an advanced debloating script. It then proceeds to download and execute the script if the user confirms their intention. This ensures that users are informed and consent to the execution of potentially impactful operations.


Code Explanation for Bloatware List

The bloatware_list is a Python list that contains tuples of bloatware applications, each identified by a unique package name and an estimated size in megabytes (MB). This list is used to keep track of applications that are considered unnecessary or undesirable, commonly referred to as "bloatware." The estimated size helps users understand how much storage space these applications occupy.

Code:

bloatware_list = [
    ("Microsoft.BingNews", 80), ("Microsoft.GetHelp", 50), ("Microsoft.Getstarted", 40), ("Microsoft.Messaging", 60),
    ("Microsoft.Microsoft3DViewer", 100), ("Microsoft.MicrosoftOfficeHub", 100), ("Microsoft.MicrosoftSolitaireCollection", 60),
    ("Microsoft.NetworkSpeedTest", 70), ("Microsoft.News", 80), ("Microsoft.Office.Lens", 100), ("Microsoft.Office.OneNote", 150),
    ("Microsoft.Office.Sway", 60), ("Microsoft.OneConnect", 70), ("Microsoft.People", 50), ("Microsoft.Print3D", 95),
    ("Microsoft.SkypeApp", 120), ("Microsoft.Office.Todo.List", 60), ("microsoft.windowscommunicationsapps", 100),
    ("Microsoft.WindowsFeedbackHub", 80), ("Microsoft.ZuneMusic", 150), ("Microsoft.ZuneVideo", 200), ("EclipseManager", 80),
    ("ActiproSoftwareLLC", 90), ("Adobe.CCExpress", 200), ("AdobeSystemsIncorporated.AdobePhotoshopExpress", 150),
    ("Clipchamp.Clipchamp", 10000), ("Facebook.Facebook", 50), ("Instagram.Instagram", 75), ("Netflix.Netflix", 120),
    ("AmazonVideo.PrimeVideo", 130), ("Microsoft.HiddenCity", 300), ("Microsoft.MixedReality.Portal", 200),
    ("ROBLOXCORPORATION.ROBLOX", 250), ("TikTok.TikTok", 90), ("Microsoft.AgeOfEmpiresCastleSiege", 180),
    ("GAMELOFTSA.Asphalt8Airborne", 400), ("king.com.BubbleWitch3Saga", 110), ("king.com.CandyCrushFriends", 140),
    ("king.com.CandyCrushSaga", 160), ("Zynga.FarmVille2CountryEscape", 150), ("Fitbit.FitbitCoach", 90),
    ("Playrix.Gardenscapes", 100), ("PhototasticCollage", 50), ("PicsArt-Photostudio", 85), ("SpotifyAB.SpotifyMusic", 110),
    ("Twitter.Twitter", 70), ("Microsoft.549981C3F5F10", 150), ("Microsoft.3DBuilder", 120), ("Microsoft.BingFinance", 60),
    ("Microsoft.BingFoodAndDrink", 40), ("Microsoft.BingHealthAndFitness", 70), ("Microsoft.BingSports", 50),
    ("Microsoft.BingTranslator", 55), ("Microsoft.BingTravel", 50), ("Microsoft.BingWeather", 45), ("Microsoft.MicrosoftJournal", 85),
    ("Microsoft.MicrosoftPowerBIForWindows", 150), ("Microsoft.Todos", 70), ("Microsoft.WindowsMaps", 100), ("Microsoft.WindowsSoundRecorder", 15), ("MSTeams", 200),
    ("ACGMediaPlayer", 80), ("AutodeskSketchBook", 150), ("CaesarsSlotsFreeCasino", 85), ("COOKINGFEVER", 100),
    ("CyberLinkMediaSuiteEssentials", 200), ("DisneyMagicKingdoms", 90), ("DrawboardPDF", 100), ("Duolingo-LearnLanguagesforFree", 70),
    ("FarmVille2CountryEscape", 150), ("Fitbit", 90), ("Flipboard", 70), ("HULULLC.HULUPLUS", 150),
    ("iHeartRadio", 80), ("LinkedInforWindows", 40), ("MarchofEmpires", 110), ("PandoraMediaInc", 60),
    ("Plex", 120), ("PolarrPhotoEditorAcademicEdition", 90), ("Royal Revolt", 100), ("Shazam", 50),
    ("Sidia.LiveWallpaper", 60), ("SlingTV", 120), ("TuneInRadio", 70), ("Viber", 75), ("WinZipUniversal", 80),
    ("Wunderlist", 50), ("XING", 60)
]

Purpose:
This list is utilized to identify and manage applications that are considered unnecessary or unwanted by the user. The estimated size helps in evaluating the impact of removing these applications on the system's storage.

Detailed Explanation:

  • Structure:
    Each entry in the list is a tuple consisting of:

    • Package Name: A string representing the application's identifier or name.
    • Size (MB): An integer representing the estimated size of the application in megabytes.
  • Applications Included:
    The list includes a wide range of applications, such as:

    • Microsoft Applications: Various built-in apps and tools from Microsoft.
    • Third-Party Software: Includes popular applications from other developers and companies.
    • Games and Entertainment: Applications related to gaming, streaming, and media.
  • Estimated Size:
    The estimated sizes provided are indicative of how much disk space these applications consume. This information is useful for determining the potential impact on system storage if these applications are removed.

Use Case:

  • System Optimization:
    The list helps in optimizing system performance by identifying and removing applications that are not needed, freeing up valuable storage space.

  • User Management:
    Provides a clear view of which applications are considered bloatware, helping users make informed decisions about which applications to uninstall.

Summary:
The bloatware_list contains tuples of applications deemed unnecessary or unwanted, along with their estimated sizes. This list is used to manage and optimize system storage by identifying and potentially removing these applications.


Code Explanation: Running PowerShell Commands

The function run_powershell_command is designed to execute a PowerShell command from within a Python script and handle its output and errors gracefully. This function uses the subprocess module to run PowerShell commands and logs the results. Here’s a detailed breakdown of the function:

Function Definition:

def run_powershell_command(command):
    try:
        result = subprocess.run(["powershell", "-Command", command], capture_output=True, text=True)
        result.check_returncode()
        logging.info(f"Command executed successfully: {command}")
        return result.stdout
    except subprocess.CalledProcessError as e:
        logging.error(f"Error executing command: {command} | Error: {e}")
        return ""

Parameters:

  • command (str): The PowerShell command you want to execute. This should be a string containing the command.

Functionality:

  1. Execution:

    • subprocess.run(...): Executes the PowerShell command.
      • ["powershell", "-Command", command]: Runs PowerShell with the specified command.
      • capture_output=True: Captures the standard output and standard error of the command.
      • text=True: Ensures that the output is returned as a string (instead of bytes).
  2. Error Handling:

    • result.check_returncode(): Checks if the command returned a non-zero exit code, which indicates an error. If an error occurred, CalledProcessError is raised.
    • logging.error(...): Logs an error message if the command fails, including the command and the error details.
  3. Success Handling:

    • logging.info(...): Logs an informational message indicating that the command was executed successfully.
    • return result.stdout: Returns the standard output of the command as a string.
  4. Error Return:

    • If an error occurs during execution, the function logs the error and returns an empty string.

Usage:

This function is useful in scenarios where you need to execute PowerShell commands from a Python script, such as:

  • System Administration: Automating administrative tasks on Windows.
  • Configuration Management: Running commands to configure or query system settings.
  • Script Integration: Integrating PowerShell commands into a larger Python-based automation framework.

Example Usage:

# Example PowerShell command to get system information
command = "Get-ComputerInfo"

# Run the PowerShell command
output = run_powershell_command(command)

# Print the output
print(output)

Notes:

  • Ensure that the logging module is properly configured in your script to capture and display log messages.
  • Handle sensitive data and commands with care, especially if they involve administrative privileges or access to system information.

This function provides a robust way to integrate PowerShell commands into Python scripts, offering clear logging and error handling for better debugging and monitoring.


Code Explanation: Checking for Installed Apps

The function check_installed_apps is designed to check for the presence of specific applications (identified as bloatware) on a Windows system. It updates a progress label to inform the user of the ongoing process and returns a list of installed bloatware applications. Here’s a detailed breakdown of the function:

Function Definition:

def check_installed_apps(progress_label):
    installed_apps = []
    progress_label.config(text="Checking for installed Bloatware...")
    root.update()  # Aktualisiert das Fenster, um die Nachricht anzuzeigen

    for app, size in bloatware_list:
        if app in run_powershell_command(f"Get-AppxPackage -Name {app}"):
            installed_apps.append((app, size))

    progress_label.config(text="")
    return installed_apps

Parameters:

  • progress_label (tk.Label): A Tkinter label widget used to display progress messages to the user.

Functionality:

  1. Initial Setup:

    • installed_apps = []: Initializes an empty list to store the installed bloatware applications.
    • progress_label.config(text="Checking for installed Bloatware..."): Updates the text of the progress_label to indicate that the checking process is starting.
    • root.update(): Refreshes the Tkinter window to immediately display the updated progress message. This is crucial for ensuring that the progress message is visible to the user before potentially time-consuming operations begin.
  2. Check for Installed Apps:

    • Iterates through each item in bloatware_list, which contains tuples of application names and their estimated sizes.
    • For each application name (app), it constructs a PowerShell command: f"Get-AppxPackage -Name {app}". This command is used to check if the application is installed.
    • if app in run_powershell_command(...): Executes the PowerShell command using the run_powershell_command function. If the command output contains the application name, it means the application is installed.
    • Appends the application and its size to the installed_apps list if it is found.
  3. Finalization:

    • progress_label.config(text=""): Clears the progress message from the label once the check is complete.
  4. Return Value:

    • return installed_apps: Returns a list of tuples where each tuple contains the name and estimated size of an installed bloatware application.

Usage:

This function is useful in scenarios where you need to:

  • Identify Installed Bloatware: Determine which applications from a predefined list of bloatware are currently installed on the system.
  • Update User Interface: Provide real-time feedback to the user about the progress of the bloatware check.

Example Usage:

# Create a Tkinter label widget for displaying progress
progress_label = tk.Label(root, text="")
progress_label.pack()

# Call the function to check for installed bloatware
installed_apps = check_installed_apps(progress_label)

# Print the list of installed bloatware apps
print(installed_apps)

Notes:

  • PowerShell Execution: The run_powershell_command function is used to execute the PowerShell commands and obtain the results.
  • User Feedback: Updating the progress_label provides immediate feedback to the user, enhancing the user experience.
  • Error Handling: Ensure that run_powershell_command handles errors gracefully, as issues with PowerShell execution could affect the results.

This function integrates application checking into a Tkinter-based GUI, providing a user-friendly way to detect installed bloatware while keeping users informed about the process.


Code Explanation: Checking for Installed Bloatware

The function check_installed_apps is used to check whether a list of specified bloatware applications are installed on a Windows system. It uses PowerShell commands to query the installed applications and updates a progress label to inform the user of the ongoing check. Here’s a detailed breakdown of the function:

Function Definition:

def check_installed_apps(progress_label):
    installed_apps = []
    progress_label.config(text="Checking for installed Bloatware...")
    root.update()  # Aktualisiert das Fenster, um die Nachricht anzuzeigen

    for app, size in bloatware_list:
        if app in run_powershell_command(f"Get-AppxPackage -Name {app}"):
            installed_apps.append((app, size))

    progress_label.config(text="")
    return installed_apps

Parameters:

  • progress_label (tk.Label): A Tkinter label widget used to display the progress of the operation to the user.

Functionality:

  1. Initialization:

    • installed_apps = []: Initializes an empty list to store the applications that are found to be installed.
  2. Update Progress Label:

    • progress_label.config(text="Checking for installed Bloatware..."): Updates the text of the progress label to inform the user that the check is in progress.
    • root.update(): Updates the Tkinter window to immediately reflect the progress message. This ensures the user interface is responsive and shows the updated message.
  3. Check for Installed Bloatware:

    • for app, size in bloatware_list:: Iterates over the list of bloatware applications and their estimated sizes.
      • run_powershell_command(f"Get-AppxPackage -Name {app}"): Executes a PowerShell command to get the package details for each application. The run_powershell_command function runs the PowerShell command and returns the output.
      • if app in run_powershell_command(...):: Checks if the application is present in the command output. If the application is found, it is added to the installed_apps list along with its size.
  4. Clear Progress Label:

    • progress_label.config(text=""): Clears the progress message from the label after the check is complete.
  5. Return Result:

    • return installed_apps: Returns the list of installed bloatware applications and their sizes.

Usage:

This function is useful in applications that need to identify and manage bloatware on a Windows system. It provides a way to:

  • Identify Installed Bloatware: Check if certain bloatware applications are installed on the system.
  • Update User Interface: Provide feedback to the user about the progress of the operation.

Example Usage:

# Example Tkinter setup
root = tk.Tk()
progress_label = tk.Label(root, text="")
progress_label.pack()

# Check for installed bloatware and get the list
installed_bloatware = check_installed_apps(progress_label)

# Display the result
print("Installed Bloatware:", installed_bloatware)

Notes:

  • Ensure that the run_powershell_command function is properly defined and imported for this function to work.
  • The function relies on PowerShell’s Get-AppxPackage command, which queries installed app packages. This command might not list all types of applications, such as traditional desktop apps, and is specific to UWP (Universal Windows Platform) apps.
  • Error handling is minimal in this function; additional error handling can be added to manage potential issues with PowerShell command execution.

This function integrates with the Tkinter user interface to provide real-time feedback and is a key component in managing installed applications in a user-friendly manner.


Code Explanation: Confirmation Dialog for Uninstallation

The confirm_uninstall function creates a confirmation dialog in a Tkinter application, prompting the user to type a specific phrase to confirm an action. The dialog appears as a separate window and requires user input before proceeding. Here’s a detailed breakdown of the function:

Function Definition:

def confirm_uninstall():
    dialog = tk.Toplevel(root)
    dialog.configure(bg="#333")
    dialog.title("Confirmation")
    tk.Label(dialog, text="Type 'Uninstall pls' to confirm", fg="white", bg="#333").pack(pady=10)
    entry = tk.Entry(dialog)
    entry.pack(pady=10)
    result = []

    def on_confirm():
        result.append(entry.get())
        dialog.destroy()

    tk.Button(dialog, text="Confirm", command=on_confirm, bg="#444", fg="white").pack(pady=10)
    dialog.transient(root)
    dialog.grab_set()
    root.wait_window(dialog)
    
    return result[0] if result else ""

Functionality:

  1. Create a Toplevel Window:

    • dialog = tk.Toplevel(root): Creates a new top-level window (Toplevel) that appears as a modal dialog on top of the main application window (root).
  2. Configure Dialog:

    • dialog.configure(bg="#333"): Sets the background color of the dialog to a dark gray (#333).
    • dialog.title("Confirmation"): Sets the title of the dialog window to "Confirmation".
  3. Add Confirmation Label:

    • tk.Label(dialog, text="Type 'Uninstall pls' to confirm", fg="white", bg="#333").pack(pady=10): Adds a label to the dialog with instruction text and sets the foreground color to white and the background color to match the dialog's background.
  4. Add Entry Widget:

    • entry = tk.Entry(dialog): Creates an entry widget where the user can type their input.
    • entry.pack(pady=10): Packs the entry widget with padding to add space around it.
  5. Handle Confirmation:

    • result = []: Initializes an empty list to store the user’s input.
    • def on_confirm(): Defines a nested function to handle the confirmation button click.
      • result.append(entry.get()): Retrieves the text entered by the user and appends it to the result list.
      • dialog.destroy(): Closes the dialog window.
  6. Add Confirm Button:

    • tk.Button(dialog, text="Confirm", command=on_confirm, bg="#444", fg="white").pack(pady=10): Creates a button labeled "Confirm" that triggers the on_confirm function when clicked. The button is styled with a dark gray background and white text.
  7. Modal Behavior:

    • dialog.transient(root): Makes the dialog transient to the main window (root), ensuring it stays on top of the main window.
    • dialog.grab_set(): Ensures that the user must interact with the dialog before returning to the main window. This makes the dialog modal.
    • root.wait_window(dialog): Blocks the main application until the dialog window is closed.
  8. Return User Input:

    • return result[0] if result else "": Returns the text entered by the user if the dialog was confirmed; otherwise, returns an empty string.

Usage:

This function is useful for confirming critical actions, such as uninstalling an application, by requiring the user to enter a specific confirmation phrase. It provides a simple mechanism for verifying user intent before proceeding with potentially destructive operations.

Example Usage:

# Example usage of confirm_uninstall function
confirmation_text = confirm_uninstall()
if confirmation_text == "Uninstall pls":
    print("User confirmed the uninstallation.")
else:
    print("User did not confirm or canceled the action.")

Notes:

  • The dialog requires user input to match a specific phrase ("Uninstall pls") to proceed. You can modify this phrase based on your requirements.
  • The wait_window method ensures that the function waits until the dialog is closed before returning, making it synchronous and blocking until the user has interacted with the dialog.
  • Ensure that the root Tkinter window is properly defined and initialized in the main application for this function to work as expected.

This function enhances user interaction by providing a clear and straightforward way to confirm critical actions in a Tkinter-based GUI application.


Bloatware Uninstaller: rm_Bloatware Function

The rm_Bloatware function creates a Tkinter window to manage and uninstall bloatware applications. This function allows users to view a list of installed applications, select unwanted apps, and uninstall them to free up storage space and improve system performance.

Function Definition

def rm_Bloatware():
    bloatware_window = tk.Toplevel(root)
    bloatware_window.title("Bloatware Uninstaller")
    bloatware_window.geometry("500x500")  # Increases the window size
    bloatware_window.configure(bg="#333")

    progress_label = tk.Label(bloatware_window, text="", fg="white", bg="#333")
    progress_label.pack(pady=10)

    installed_apps = check_installed_apps(progress_label)

    if not installed_apps:
        tk.Label(bloatware_window, text="Congrats, you're Bloatware free :)", fg="white", bg="#333").pack(pady=20)
        tk.Button(bloatware_window, text="Close", command=bloatware_window.destroy, bg="#444", fg="white").pack(pady=20)
        return

    vars_ = [tk.IntVar() for _ in installed_apps]
    
    tk.Label(bloatware_window, text="These Apps are potential unwanted Bloatware.", fg="white", bg="#333").pack(pady=10)
    
    # Add a frame with a canvas and scrollbar for scrollable checkboxes
    frame_container = tk.Frame(bloatware_window, bg="#333")
    frame_container.pack(fill="both", expand=True)

    canvas = tk.Canvas(frame_container, bg="#333")
    scrollbar = tk.Scrollbar(frame_container, orient="vertical", command=canvas.yview)
    scrollable_frame = tk.Frame(canvas, bg="#333")

    scrollable_frame.bind(
        "<Configure>",
        lambda e: canvas.configure(
            scrollregion=canvas.bbox("all")
        )
    )

    canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
    canvas.configure(yscrollcommand=scrollbar.set)

    canvas.pack(side="left", fill="both", expand=True)
    scrollbar.pack(side="right", fill="y")

    # Populate the scrollable frame with checkboxes
    for i, (app, size) in enumerate(installed_apps):
        chk = tk.Checkbutton(scrollable_frame, text=f"{app} ({size} MB)", variable=vars_[i], 
                             fg="white", bg="#333", selectcolor="#444", activebackground="#555")
        chk.grid(row=i, column=0, sticky='w', padx=20, pady=2)

    label_info = tk.Label(bloatware_window, text="", fg="white", bg="#333")
    label_info.pack(pady=10)

    def on_check():
        selected = [(app, size) for i, (app, size) in enumerate(installed_apps) if vars_[i].get() == 1]
        total_size = sum(size for _, size in selected)
        label_info.config(text=f"You will have more than {total_size} MB more free Storage, and your PC will be faster.")
    
    def on_uninstall():
        checked_apps = [(app, size) for i, (app, size) in enumerate(installed_apps) if vars_[i].get() == 1]
        if not checked_apps:
            messagebox.showwarning("No Selection", "No apps selected for uninstallation.")
            return

        on_check()  # Update the info label before confirmation
        confirmation = confirm_uninstall()
        if confirmation == "Uninstall pls":
            uninstall_selected_apps(checked_apps)
            remaining_apps = check_installed_apps(progress_label)
            if not any(app in [a[0] for a in remaining_apps] for app, _ in checked_apps):
                messagebox.showinfo("Uninstallation Complete", "Selected apps have been uninstalled.")
            else:
                messagebox.showerror("Uninstallation Failed", "Some apps could not be uninstalled.")
            bloatware_window.destroy()
        else:
            messagebox.showinfo("Uninstallation Cancelled", "No apps were uninstalled.")

    tk.Button(bloatware_window, text="Uninstall selected", command=on_uninstall, bg="#444", fg="white").pack(pady=20)
    tk.Button(bloatware_window, text="Advanced Debloat+Stop Tracking", command=advanced_debloat, bg="#444", fg="white").pack(pady=20)

Functionality Breakdown

1. Create a New Window:

  • bloatware_window = tk.Toplevel(root): Initializes a new top-level window for managing bloatware, separate from the main application window.
  • bloatware_window.title("Bloatware Uninstaller"): Sets the title of the window to "Bloatware Uninstaller".
  • bloatware_window.geometry("500x500"): Specifies the window size to 500x500 pixels.
  • bloatware_window.configure(bg="#333"): Sets the background color of the window to a dark grey (#333), providing a consistent and modern look.

2. Progress Label:

  • progress_label = tk.Label(...): Creates a label widget to show progress messages during the uninstallation process.
  • progress_label.pack(pady=10): Packs the label into the window with padding for spacing.

3. Check Installed Apps:

  • installed_apps = check_installed_apps(progress_label): Calls the function check_installed_apps, passing the progress_label to dynamically update progress as it checks for installed apps.
  • if not installed_apps:: Checks if there are no installed apps that qualify as bloatware.
    • If true, displays a congratulatory message and a close button to exit the window.

4. Display Installed Apps:

  • Checkbox Management:
    • vars_ = [tk.IntVar() for _ in installed_apps]: Creates a list of IntVar objects to keep track of the state (checked or unchecked) of each app checkbox.
    • tk.Label(...): Displays a label indicating the purpose of the checkboxes.

5. Scrollable Frame for Checkboxes:

  • A frame is created with a canvas and a scrollbar to allow users to scroll through a potentially long list of applications.
    • frame_container, canvas, scrollbar, scrollable_frame: These widgets work together to provide a scrollable area.
    • scrollable_frame.bind(...): Updates the scroll region when the frame is resized or configured.
    • canvas.create_window(...): Embeds the scrollable frame within the canvas, allowing it to scroll.
    • scrollbar.pack(...): Packs the scrollbar on the right side of the container frame.

6. Populate Scrollable Frame with Checkboxes:

  • for i, (app, size) in enumerate(installed_apps):: Iterates through the list of installed apps and their sizes.
  • tk.Checkbutton(...): Creates a checkbutton for each app, allowing users to select apps for uninstallation.
  • chk.grid(...): Places each checkbutton in the scrollable frame with appropriate styling.

7. Info Label:

  • label_info: A label that dynamically updates to display the total size of selected apps and the amount of storage that will be freed up.
  • label_info.pack(pady=10): Packs the label into the window with padding.

8. Handle Check Button:

  • on_check Function:
    • selected = ...: Retrieves the list of selected apps and calculates their total size.
    • label_info.config(...): Updates the label_info with the calculated storage savings.

9. Handle Uninstall Button:

  • on_uninstall Function:
    • checked_apps = ...: Retrieves a list of apps selected for uninstallation.
    • messagebox.showwarning(...): Displays a warning if no apps are selected.
    • on_check(): Calls on_check to update the information label before proceeding.
    • confirm_uninstall(): Shows a confirmation dialog to the user.
    • uninstall_selected_apps(checked_apps): Uninstalls the selected apps.
    • Error and Success Messages: Displays appropriate messages based on the success or failure of the uninstallation process.

10. Additional Buttons:

  • "Uninstall selected" Button: Initiates the uninstallation process by calling on_uninstall.
  • "Advanced Debloat+Stop Tracking" Button: Calls the advanced_debloat function for additional system optimization actions.

Additional Functions Required:

  • check_installed_apps(progress_label): Function that scans the system for installed apps that are potential bloatware.
  • confirm_uninstall(): Function that displays a dialog box to confirm the uninstallation of selected apps.
  • uninstall_selected_apps(checked_apps): Function that handles the uninstallation of

apps selected by the user.

  • advanced_debloat(): Function that provides advanced debloating options and stops tracking processes.

Usage:

To use the rm_Bloatware function, call it from your main application or any other appropriate place in your codebase. It will open a new window allowing users to select and uninstall unwanted applications from their system, thereby freeing up storage and improving performance.

# Example of how to invoke the rm_Bloatware function
rm_Bloatware()

By incorporating this function, users can easily manage bloatware on their computers, enhancing their system's performance and freeing up valuable storage space.


Setting Up the GUI:

import tkinter as tk

# Initialize the main application window
root = tk.Tk()

# Set the title of the window
root.title("PC Optimus")

# Set the size of the window
root.geometry("400x600")

# Set the background color of the window
root.configure(bg="#2E2E2E")

Adding Widgets:

To make the GUI functional, you will need to add buttons and other widgets to the root window. Here's how you can add some sample buttons for different functionalities:

# Define buttons for different functionalities
info_button = tk.Button(root, text="System Info", command=show_info_menu, bg="#444", fg="white")
clean_button = tk.Button(root, text="Clean", command=show_clean_menu, bg="#444", fg="white")
update_button = tk.Button(root, text="Update", command=show_update_menu, bg="#444", fg="white")
repair_button = tk.Button(root, text="Repair", command=show_repair_menu, bg="#444", fg="white")
help_button = tk.Button(root, text="Help", command=lambda: messagebox.showinfo("Help", "Help information here"), bg="#444", fg="white")

# Pack the buttons onto the window
info_button.pack(pady=10)
clean_button.pack(pady=10)
update_button.pack(pady=10)
repair_button.pack(pady=10)
help_button.pack(pady=10)

Adding Menu Functions:

Ensure that you have the menu functions such as show_info_menu, show_clean_menu, show_update_menu, and show_repair_menu defined. These functions should change the content of the main window when their respective buttons are clicked.

Main Loop:

Finally, start the Tkinter event loop to display the window and handle user interactions:

# Start the Tkinter event loop
root.mainloop()

Putting It All Together:

Here’s a complete example, combining the setup of the root window and adding a few widgets:

import tkinter as tk
from tkinter import messagebox

# Initialize the main application window
root = tk.Tk()

# Set the title of the window
root.title("PC Optimus")

# Set the size of the window
root.geometry("400x600")

# Set the background color of the window
root.configure(bg="#2E2E2E")

# Define functions for menu buttons
def show_info_menu():
    clear_frame()
    systeminfo_button.pack(pady=10)
    advanced_systeminfo_button.pack(pady=10)
    resource_monitoring_button.pack(pady=10)
    speedtest_button.pack(pady=10)
    connection_button.pack(pady=10)
    back_button.pack(pady=10)

def show_clean_menu():
    clear_frame()
    rm_bloatware_button.pack(pady=10)
    clean_mngr_button.pack(pady=10)
    wsreset_button.pack(pady=10)
    disk_cleanup_button.pack(pady=10)
    temp_cleanup_button.pack(pady=10)
    prefetch_clean_button.pack(pady=10)
    clean_invis_button.pack(pady=10)
    defragment_button.pack(pady=10)
    clean_vs_button.pack(pady=10)
    Empty_RecycleBin_button.pack(pady=10)
    back_button.pack(pady=10)

def show_update_menu():
    clear_frame()
    update_apps_button.pack(pady=10)
    windows_update_button.pack(pady=10)
    driver_update_button.pack(pady=10)
    back_button.pack(pady=10)

def show_repair_menu():
    clear_frame()
    health_scan_button.pack(pady=10)
    storage_diagonistics_button.pack(pady=10)
    repair_file_system_button.pack(pady=10)
    repair_connection_button.pack(pady=10)
    back_button.pack(pady=10)

def clear_frame():
    for widget in root.winfo_children():
        widget.pack_forget()

# Define buttons for different functionalities
info_button = tk.Button(root, text="System Info", command=show_info_menu, bg="#444", fg="white")
clean_button = tk.Button(root, text="Clean", command=show_clean_menu, bg="#444", fg="white")
update_button = tk.Button(root, text="Update", command=show_update_menu, bg="#444", fg="white")
repair_button = tk.Button(root, text="Repair", command=show_repair_menu, bg="#444", fg="white")
help_button = tk.Button(root, text="Help", command=lambda: messagebox.showinfo("Help", "Help information here"), bg="#444", fg="white")

# Pack the buttons onto the window
info_button.pack(pady=10)
clean_button.pack(pady=10)
update_button.pack(pady=10)
repair_button.pack(pady=10)
help_button.pack(pady=10)

# Start the Tkinter event loop
root.mainloop()

Summary:

  1. Initialization: Create the main window with tk.Tk(), set its title, size, and background color.
  2. Add Widgets: Define and pack buttons and other widgets.
  3. Define Menu Functions: Implement functions to update the content of the window based on user interactions.
  4. Run Event Loop: Start Tkinter’s event loop with root.mainloop() to display the GUI.

Feel free to adjust the widget properties and add additional functionality based on your specific requirements.


Here’s a general breakdown of how buttons are created and configured in a this Code with a Tkinter GUI.

Pattern for Creating and Configuring Buttons:

  1. Button Creation:

    button_name = tk.Button(
        root,  # The parent widget, usually the main window or a frame.
        text="Button Text",  # The text displayed on the button.
        command=button_command,  # The function to call when the button is clicked.
        bg="#444",  # The background color of the button.
        fg="white",  # The text color of the button.
        activebackground="#555",  # The background color when the button is pressed.
        activeforeground="white",  # The text color when the button is pressed.
        borderwidth=1,  # The width of the button border.
        relief="raised"  # The style of the button border.
    )
  2. Configuring Button Styles:

    • bg: Sets the background color of the button. A hex color code is used here (#444 for a dark gray).
    • fg: Sets the color of the button's text. Here it is set to white.
    • activebackground: Defines the background color when the button is actively pressed. A slightly different shade is used (#555).
    • activeforeground: Sets the color of the text when the button is pressed. Again, it's white.
    • borderwidth: Specifies the width of the button's border.
    • relief: Defines the 3D effect of the button’s border. Options include raised, sunken, flat, etc.
  3. Command Function:

    • command: Associates the button with a function that gets called when the button is clicked. You can pass a function directly or use a lambda function to pass parameters.
  4. Adding Button to the Window: After creating the button, you use methods like pack(), grid(), or place() to position the button in the window or frame. For example:

    button_name.pack(pady=10)  # Places the button using the pack geometry manager with padding around it.

Example Breakdown:

Here’s how the button creation pattern works for a specific button:

repair_file_system_button = tk.Button(
    root,  # Parent widget (the main window).
    text="Repair Filesystem",  # Text displayed on the button.
    command=lambda: run_admin_command("echo J | chkdsk /f", "Successfully initiated chkdsk, your File System will be checked on the next startup."),  # Function to run when clicked.
    bg="#444",  # Button background color.
    fg="white",  # Button text color.
    activebackground="#555",  # Background color when pressed.
    activeforeground="white",  # Text color when pressed.
    borderwidth=1,  # Border width.
    relief="raised"  # Border style.
)

Usage and Organization:

  1. Consistency: Ensure that all buttons follow a consistent style for a cohesive look. This means using similar color schemes, border styles, and text colors.

  2. Functionality: Each button is linked to a specific function (command) which defines what happens when the button is clicked. This could be opening a new window, executing a command, or displaying information.

  3. Layout Management: Use layout management methods (pack, grid, place) to position buttons. For example, you might use pack(pady=10) to add vertical padding around a button, ensuring that buttons have appropriate spacing.

  4. Dynamic Content: Some buttons use lambda to pass arguments to their command functions. This is useful for buttons that need to execute commands with specific parameters.

By following this pattern, you can create a well-organized and visually consistent set of buttons for your Tkinter GUI application. Each button can trigger different functions or commands, helping to manage various aspects of the application efficiently.


The lines show_main_menu() and root.mainloop() are used to start and display your Tkinter GUI application. Here’s a detailed explanation of what these lines do:

show_main_menu()

show_main_menu()
  • Purpose: This function is typically used to set up and display the main menu of your application. It organizes and places buttons or widgets that represent the primary functionalities of your GUI.
  • Function Call: When show_main_menu() is called, it clears the current content of the main window (usually to remove any previous views) and then packs (or places) the main menu buttons. This ensures that the user sees the main menu when the application starts.

root.mainloop()

root.mainloop()
  • Purpose: This is the main event loop of your Tkinter application. It waits for user interactions (such as clicks, key presses, etc.) and updates the GUI accordingly.
  • Function: root.mainloop() is a blocking call, meaning that it will keep running and will not return until the application window is closed. During this time, it continuously listens for events and processes them.

Putting It Together:

  1. Initialize and Configure GUI: You set up and configure your Tkinter window (root) and create widgets (buttons, labels, etc.) to be displayed.

  2. Display Main Menu: show_main_menu() is called to set the initial state of the window to show the main menu. This function typically arranges and displays the buttons that allow users to navigate to different parts of your application.

  3. Start Event Loop: root.mainloop() starts the Tkinter event loop, keeping the window open and responsive. It processes all events and user interactions until the user closes the window.

Example Code:

Here’s an example of how these lines fit into a typical Tkinter application:

import tkinter as tk

def show_main_menu():
    clear_frame()  # Assuming you have a function to clear the frame
    info_button.pack(pady=10)
    clean_button.pack(pady=10)
    update_button.pack(pady=10)
    repair_button.pack(pady=10)
    help_button.pack(pady=10)

create_shortcut()

# Create the main application window
root = tk.Tk()
root.title("PC Optimus")
root.geometry("400x600")
root.configure(bg="#2E2E2E")

# Create and configure buttons and other widgets
info_button = tk.Button(root, text="Info", command=show_info_menu, bg="#444", fg="white", activebackground="#555", activeforeground="white", borderwidth=1, relief="raised")
clean_button = tk.Button(root, text="Clean", command=show_clean_menu, bg="#444", fg="white", activebackground="#555", activeforeground="white", borderwidth=1, relief="raised")
update_button = tk.Button(root, text="Update", command=show_update_menu, bg="#444", fg="white", activebackground="#555", activeforeground="white", borderwidth=1, relief="raised")
repair_button = tk.Button(root, text="Repair", command=show_repair_menu, bg="#444", fg="white", activebackground="#555", activeforeground="white", borderwidth=1, relief="raised")
help_button = tk.Button(root, text="Help", command=show_help, bg="#444", fg="white", activebackground="#555", activeforeground="white", borderwidth=1, relief="raised")

# Display the main menu
show_main_menu()

# Start the Tkinter event loop
root.mainloop()

In summary, show_main_menu() initializes and displays the main interface of your application, while root.mainloop() keeps the application running and responsive to user interactions. Together, they form the core of a Tkinter application’s execution flow.


I hope this helped you to understand my Code :)

Clone this wiki locally