Dynamic Resource Optimizer (DRO) Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk, messagebox
import psutil
import time
import threading

class ResourceOptimizer:
    def __init__(self, master):
        self.master = master
        master.title("Dynamic Resource Optimizer (DRO)")
        master.geometry("600x400")

        # Style
        self.style = ttk.Style()
        self.style.theme_use('clam')  # 'clam', 'alt', 'default', 'classic'
        self.style.configure('TButton', padding=6, relief='raised')
        self.style.configure('TLabel', padding=6)
        self.style.configure('TProgressbar', thickness=20)

        # Variables
        self.cpu_usage = tk.DoubleVar(value=0.0)
        self.memory_usage = tk.DoubleVar(value=0.0)
        self.disk_usage = tk.DoubleVar(value=0.0)
        self.is_running = False

        # Labels and Progress Bars
        ttk.Label(master, text="CPU Usage:").grid(row=0, column=0, sticky=tk.W, padx=10, pady=5)
        self.cpu_progress = ttk.Progressbar(master, orient=tk.HORIZONTAL, length=300, mode='determinate', variable=self.cpu_usage)
        self.cpu_progress.grid(row=0, column=1, sticky=tk.W, padx=10, pady=5)
        self.cpu_label = ttk.Label(master, text="0.0%")
        self.cpu_label.grid(row=0, column=2, sticky=tk.W, padx=5, pady=5)

        ttk.Label(master, text="Memory Usage:").grid(row=1, column=0, sticky=tk.W, padx=10, pady=5)
        self.memory_progress = ttk.Progressbar(master, orient=tk.HORIZONTAL, length=300, mode='determinate', variable=self.memory_usage)
        self.memory_progress.grid(row=1, column=1, sticky=tk.W, padx=10, pady=5)
        self.memory_label = ttk.Label(master, text="0.0%")
        self.memory_label.grid(row=1, column=2, sticky=tk.W, padx=5, pady=5)

        ttk.Label(master, text="Disk Usage:").grid(row=2, column=0, sticky=tk.W, padx=10, pady=5)
        self.disk_progress = ttk.Progressbar(master, orient=tk.HORIZONTAL, length=300, mode='determinate', variable=self.disk_usage)
        self.disk_progress.grid(row=2, column=1, sticky=tk.W, padx=10, pady=5)
        self.disk_label = ttk.Label(master, text="0.0%")
        self.disk_label.grid(row=2, column=2, sticky=tk.W, padx=5, pady=5)

        # Buttons
        self.start_button = ttk.Button(master, text="Start Monitoring", command=self.start_monitoring)
        self.start_button.grid(row=3, column=0, columnspan=3, pady=10)

        self.stop_button = ttk.Button(master, text="Stop Monitoring", command=self.stop_monitoring, state=tk.DISABLED)
        self.stop_button.grid(row=4, column=0, columnspan=3, pady=10)

        self.details_button = ttk.Button(master, text="Details", command=self.show_details)
        self.details_button.grid(row=5, column=0, columnspan=3, pady=10)

    def update_resource_usage(self):
        while self.is_running:
            cpu_percent = psutil.cpu_percent(interval=1)
            memory_percent = psutil.virtual_memory().percent
            disk_percent = psutil.disk_usage('/').percent

            self.cpu_usage.set(cpu_percent)
            self.memory_usage.set(memory_percent)
            self.disk_usage.set(disk_percent)

            self.cpu_label.config(text=f"{cpu_percent:.1f}%")
            self.memory_label.config(text=f"{memory_percent:.1f}%")
            self.disk_label.config(text=f"{disk_percent:.1f}%")

            self.cpu_progress['value'] = cpu_percent
            self.memory_progress['value'] = memory_percent
            self.disk_progress['value'] = disk_percent

            time.sleep(0.5)

    def start_monitoring(self):
        self.is_running = True
        self.start_button.config(state=tk.DISABLED)
        self.stop_button.config(state=tk.NORMAL)
        self.monitoring_thread = threading.Thread(target=self.update_resource_usage)
        self.monitoring_thread.daemon = True  # Allow the program to exit even if this thread is running
        self.monitoring_thread.start()

    def stop_monitoring(self):
        self.is_running = False
        self.start_button.config(state=tk.NORMAL)
        self.stop_button.config(state=tk.DISABLED)

    def show_details(self):
        details_text = """
        Dynamic Resource Optimizer (DRO) is a real-time system monitoring tool.

        It provides detailed information about CPU, Memory, and Disk usage, allowing
        users to visualize resource consumption.

        Key Features:
        - Real-time monitoring of CPU, Memory, and Disk usage.
        - Visual representation through progress bars.
        - Start/Stop monitoring functionality.

        This application is designed to help users understand their system's
        resource utilization and identify potential bottlenecks.
        """
        messagebox.showinfo("Details", details_text)

root = tk.Tk()
optimizer = ResourceOptimizer(root)
root.mainloop()
👁️ Viewed: 6

Comments