159 lines
5.9 KiB
Python
159 lines
5.9 KiB
Python
import tkinter as tk
|
|
from tkinter import Menu
|
|
from tkinter import ttk
|
|
import threading
|
|
import psutil
|
|
import datetime
|
|
import time
|
|
|
|
|
|
def update_bot_regions_thread(label_memory, label_sent, label_recv, label_cpu):
|
|
"""Hilo que actualiza las estadísticas del sistema."""
|
|
while True:
|
|
# Obtener estadísticas del sistema
|
|
memory_usage = psutil.virtual_memory().percent
|
|
bytes_sent_per_sec, bytes_recv_per_sec = get_network_usage()
|
|
cpu_usage = psutil.cpu_percent(interval=0)
|
|
|
|
# Actualizar etiquetas en el hilo principal usando `after`
|
|
root.after(0, label_memory.config, {"text": f"Uso RAM: {memory_usage:.2f}%"})
|
|
root.after(0, label_sent.config, {"text": f"Subida: {bytes_sent_per_sec / 1024:.2f} KB/s"})
|
|
root.after(0, label_recv.config, {"text": f"Descarga: {bytes_recv_per_sec / 1024:.2f} KB/s"})
|
|
root.after(0, label_cpu.config, {"text": f"Uso CPU: {cpu_usage:.2f}%"})
|
|
|
|
time.sleep(1) # Esperar 1 segundo antes de la próxima actualización
|
|
|
|
|
|
def get_network_usage():
|
|
"""Calcula la velocidad de subida y bajada de la red."""
|
|
net_io_start = psutil.net_io_counters()
|
|
time.sleep(1) # Esperar 1 segundo para medir el uso
|
|
net_io_end = psutil.net_io_counters()
|
|
|
|
# Calcular velocidades
|
|
bytes_sent_per_sec = net_io_end.bytes_sent - net_io_start.bytes_sent
|
|
bytes_recv_per_sec = net_io_end.bytes_recv - net_io_start.bytes_recv
|
|
|
|
return bytes_sent_per_sec, bytes_recv_per_sec
|
|
|
|
|
|
def update_time(label_fecha_hora):
|
|
"""Actualiza la hora y el día de la semana en un label."""
|
|
# Obtener la fecha y hora actual
|
|
now = datetime.datetime.now()
|
|
day_of_week = now.strftime("%A") # Día de la semana
|
|
time_str = now.strftime("%H:%M:%S") # Hora en formato HH:MM:SS
|
|
date_str = now.strftime("%Y-%m-%d") # Fecha en formato YYYY-MM-DD
|
|
label_text = f"{day_of_week}, {date_str} - {time_str}"
|
|
|
|
# Actualizar el label
|
|
label_fecha_hora.config(text=label_text)
|
|
|
|
# Programar la próxima actualización después de 1 segundo
|
|
root.after(1000, update_time, label_fecha_hora)
|
|
|
|
|
|
# Crear la ventana principal
|
|
root = tk.Tk()
|
|
root.title("Ventana Responsive")
|
|
root.geometry("1000x700") # Tamaño inicial
|
|
|
|
# Configurar la ventana principal para que sea responsive
|
|
root.columnconfigure(0, weight=0) # Columna izquierda, tamaño fijo
|
|
root.columnconfigure(1, weight=1) # Columna central, tamaño variable
|
|
root.columnconfigure(2, weight=0) # Columna derecha, tamaño fijo
|
|
root.rowconfigure(0, weight=1) # Fila principal, tamaño variable
|
|
root.rowconfigure(1, weight=0) # Barra de estado, tamaño fijo
|
|
|
|
|
|
# Crear el menú superior
|
|
menu_bar = Menu(root)
|
|
|
|
file_menu = Menu(menu_bar, tearoff=0)
|
|
file_menu.add_command(label="Nuevo")
|
|
file_menu.add_command(label="Abrir")
|
|
file_menu.add_separator()
|
|
file_menu.add_command(label="Salir", command=root.quit)
|
|
|
|
edit_menu = Menu(menu_bar, tearoff=0)
|
|
edit_menu.add_command(label="Copiar")
|
|
edit_menu.add_command(label="Pegar")
|
|
|
|
help_menu = Menu(menu_bar, tearoff=0)
|
|
help_menu.add_command(label="Acerca de")
|
|
|
|
menu_bar.add_cascade(label="Archivo", menu=file_menu)
|
|
menu_bar.add_cascade(label="Editar", menu=edit_menu)
|
|
menu_bar.add_cascade(label="Ayuda", menu=help_menu)
|
|
|
|
root.config(menu=menu_bar)
|
|
|
|
# Crear frames
|
|
frame_izquierdo = tk.Frame(root, bg="lightblue", width=200)
|
|
frame_central = tk.Frame(root, bg="white")
|
|
frame_derecho = tk.Frame(root, bg="lightgreen", width=200)
|
|
|
|
frame_izquierdo.grid(row=0, column=0, sticky="ns")
|
|
frame_central.grid(row=0, column=1, sticky="nsew")
|
|
frame_derecho.grid(row=0, column=2, sticky="ns")
|
|
|
|
frame_izquierdo.grid_propagate(False)
|
|
frame_derecho.grid_propagate(False)
|
|
|
|
|
|
# Dividir el frame central en dos partes (superior variable e inferior fija)
|
|
frame_central.rowconfigure(0, weight=1) # Parte superior, tamaño variable
|
|
frame_central.rowconfigure(1, weight=0) # Parte inferior, tamaño fijo
|
|
frame_central.columnconfigure(0, weight=1) # Ocupa toda la anchura
|
|
|
|
# Crear subframes dentro del frame central
|
|
frame_superior = tk.Frame(frame_central, bg="lightyellow")
|
|
frame_inferior = tk.Frame(frame_central, bg="lightgray", height=100)
|
|
|
|
# Colocar los subframes dentro del frame central
|
|
frame_superior.grid(row=0, column=0, sticky="nsew")
|
|
frame_inferior.grid(row=1, column=0, sticky="ew")
|
|
|
|
# Fijar el tamaño de la parte inferior
|
|
frame_inferior.grid_propagate(False)
|
|
# Crear la barra de estado
|
|
barra_estado = tk.Label(root, text="Barra de estado", bg="lightgray", anchor="w")
|
|
barra_estado.grid(row=1, column=0, columnspan=3, sticky="ew")
|
|
|
|
|
|
# Notebook para las pestañas
|
|
|
|
style = ttk.Style()
|
|
style.configure("CustomNotebook.TNotebook.Tab", font=("Arial", 12, "bold"))
|
|
notebook = ttk.Notebook(frame_superior, style="CustomNotebook.TNotebook")
|
|
notebook.pack(fill="both", expand=True)
|
|
|
|
|
|
# Crear cinco solapas
|
|
for i in range(1, 6):
|
|
tab = ttk.Frame(notebook)
|
|
|
|
notebook.add(tab, text=f"Solapa {i}", padding=4)
|
|
# Añadir un Label en cada solapa para diferenciarla
|
|
label = ttk.Label(tab, text=f"Contenido de la Solapa {i}")
|
|
label.pack(pady=10)
|
|
|
|
label_cpu = tk.Label(barra_estado, text="Uso CPU", bg="orange", anchor="w", width=20)
|
|
label_bytes_recv = tk.Label(barra_estado, text="Descarga", bg="blue", anchor="w", width=20)
|
|
label_bytes_sent = tk.Label(barra_estado, text="Subida", bg="cyan", anchor="w", width=20)
|
|
label_uso_memoria = tk.Label(barra_estado, text="Uso RAM", bg="pink", anchor="w", width=20)
|
|
label_fecha_hora = tk.Label(barra_estado, text="Hilo fecha-hora", font=("Helvetica", 14), fg="blue", relief="sunken", anchor="w", width=20)
|
|
|
|
label_cpu.pack(side="left", fill="x", expand=True)
|
|
label_bytes_recv.pack(side="left", fill="x", expand=True)
|
|
label_bytes_sent.pack(side="left", fill="x", expand=True)
|
|
label_uso_memoria.pack(side="left", fill="x", expand=True)
|
|
label_fecha_hora.pack(side="right", fill="x", expand=True)
|
|
|
|
# Iniciar actualizaciones en hilos separados
|
|
threading.Thread(target=update_bot_regions_thread, args=(label_uso_memoria, label_bytes_sent, label_bytes_recv, label_cpu), daemon=True).start()
|
|
update_time(label_fecha_hora)
|
|
|
|
# Ejecutar la aplicación
|
|
root.mainloop()
|