Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import os
- from tkinter import ttk
- import time
- import sys
- from threading import Thread
- try:
- import psutil
- except ModuleNotFoundError:
- os.system("pip install psutil")
- import psutil
- sys.setrecursionlimit(10**6)
- _root = frame1
- topbar = tk.Frame(_root, bd=0, bg='white')
- content = tk.Frame(_root, bd=0, bg='white')
- topbar.pack(fill='x')
- content.pack(expand=True, fill='both')
- currentscreen = "ut"
- def utilization():
- global currentscreen
- global tk
- global ttk
- global content
- global time
- global Thread
- currentscreen = "ut"
- try:
- global refreshbutton
- refreshbutton.destroy()
- except:
- pass
- for child in content.winfo_children():
- child.destroy()
- cpucolor = "deepskyblue"
- memcolor = "purple1"
- dskcolor = "green3"
- sty = ttk.Style()
- sty.theme_use('winnative')
- sty.configure("cpu.Horizontal.TProgressbar", background=cpucolor, foreground=cpucolor, darkcolor=cpucolor, lightcolor=cpucolor, bordercolor=cpucolor, relief='solid')
- sty.configure("mem.Horizontal.TProgressbar", background=memcolor, foreground=memcolor, darkcolor=memcolor, lightcolor=memcolor, bordercolor=memcolor, relief='solid')
- sty.configure("dsk.Horizontal.TProgressbar", background=dskcolor, foreground=dskcolor, darkcolor=dskcolor, lightcolor=dskcolor, bordercolor=dskcolor, relief='solid')
- side1 = tk.Frame(content, bd=0, bg='white', padx=10)
- side2 = tk.Frame(content, bd=0, bg='white', padx=10)
- side1.pack(side="left", expand=True, fill="both")
- side2.pack(side="left", expand=True, fill="both")
- corner1 = tk.Frame(side1, bd=0, bg='white')
- corner2 = tk.Frame(side2, bd=0, bg='white')
- corner3 = tk.Frame(side2, bd=0, bg='white')
- corner1.pack(expand=True, fill="both")
- corner2.pack(expand=True, fill="both")
- corner3.pack(expand=True, fill="both")
- cpulabel = tk.Label(corner1, bg='white', text="CPU", font='TkDefaultFont 20')
- cpulabel.pack(anchor='nw')
- cpuamount = ttk.Progressbar(corner1, style="cpu.Horizontal.TProgressbar", orient="horizontal")
- cpuamount.pack(fill="x")
- cpupercentage = tk.Label(corner1, bg='white', text="0%")
- cpupercentage.pack(anchor='nw')
- memlabel = tk.Label(corner2, bg='white', text="Memory", font='TkDefaultFont 20')
- memlabel.pack(anchor='nw')
- memamount = ttk.Progressbar(corner2, style="mem.Horizontal.TProgressbar", orient="horizontal")
- memamount.pack(fill="x")
- mempercentage = tk.Label(corner2, bg='white', text="0% out of 0GB")
- mempercentage.pack(anchor='nw')
- dsklabel = tk.Label(corner3, bg='white', text="Disk", font='TkDefaultFont 20')
- dsklabel.pack(anchor='nw')
- dskamount = ttk.Progressbar(corner3, style="dsk.Horizontal.TProgressbar", orient="horizontal")
- dskamount.pack(fill="x")
- dsktotal = tk.Label(corner3, bg='white', text="0B")
- dsktotal.pack(anchor='nw')
- def update_ut():
- global currentscreen
- def humanbytes(B):
- """Return the given bytes as a human friendly KB, MB, GB, or TB string"""
- B = float(B)
- KB = float(1024)
- MB = float(KB ** 2) # 1,048,576
- GB = float(KB ** 3) # 1,073,741,824
- TB = float(KB ** 4) # 1,099,511,627,776
- if B < KB:
- return '{0} {1}'.format(B, 'Bytes' if 0 == B > 1 else 'Byte')
- elif KB <= B < MB:
- return '{0:.2f} KB'.format(B / KB)
- elif MB <= B < GB:
- return '{0:.2f} MB'.format(B / MB)
- elif GB <= B < TB:
- return '{0:.2f} GB'.format(B / GB)
- elif TB <= B:
- return '{0:.2f} TB'.format(B / TB)
- while currentscreen == "ut":
- try:
- nonlocal cpuamount, cpupercentage, memamount, mempercentage, dsktotal, dskamount
- global psutil, time
- cpupercent = psutil.cpu_percent()
- vmem = psutil.virtual_memory()
- membytes = vmem.total
- totalmem = humanbytes(membytes)
- memused = humanbytes(vmem.used)
- mempercent = vmem.percent
- dsku = psutil.disk_usage("C:")
- dskbytes = dsku.total
- dskused = dsku.used
- dskpercent = dsku.percent
- totaldsk = humanbytes(dskbytes)
- useddsk = humanbytes(dskused)
- cpuamount.config(value=cpupercent)
- cpupercentage.config(text=f"{cpupercent}%")
- memamount.config(value=mempercent)
- mempercentage.config(text=f"{memused} out of {totalmem} ({mempercent}%)")
- dskamount.config(value=dskpercent)
- dsktotal.config(text=f"{useddsk} used of {totaldsk} ({dskpercent}%)")
- time.sleep(1)
- except:
- break
- Thread(target=update_ut, daemon=True).start()
- def processes():
- global tk
- global psutil
- global content
- global topbar
- global Thread
- global currentscreen
- currentscreen = "pr"
- for child in content.winfo_children():
- child.destroy()
- canvas = tk.Canvas(content, bg='white', bd=0, highlightthickness=0)
- scrollbar = tk.Scrollbar(content, orient="vertical", command=canvas.yview, troughcolor='white', width=12)
- processlist = tk.Frame(canvas, bd=0, bg='white', highlightcolor='white', highlightthickness=0)
- processlist.bind(
- "<Configure>",
- lambda e: canvas.configure(
- scrollregion=canvas.bbox("all")
- )
- )
- canvas.create_window((0, 0), window=processlist, anchor="nw")
- canvas.configure(yscrollcommand=scrollbar.set)
- def update_pr():
- import collections
- global time
- procs = []
- for proc in psutil.process_iter(['name']):
- procs.append(proc.info['name'])
- procs = sorted(procs, key=str.lower)
- procscount = collections.Counter(procs)
- for child in processlist.winfo_children():
- child.destroy()
- for i in procscount:
- if procscount[i] > 1:
- tk.Label(processlist, text=f"{i} ({str(procscount[i])})", bg='white', justify='left').pack(anchor='nw')
- else:
- tk.Label(processlist, text=i, bg='white', justify='left').pack(anchor='nw')
- scrollbar.update()
- canvas.place(x=0, y=0, relwidth=1.0, relheight=1.0)
- scrollbar.pack(side="right", fill="y")
- global refreshbutton
- refreshbutton = tk.Button(topbar, text='Refresh', bg='white', bd=0, command=update_pr)
- refreshbutton.pack(anchor='e')
- update_pr()
- pr_button = tk.Button(topbar, bd=0, bg='white', relief='solid', text='Processes', command=processes)
- pr_button.pack(side='left', anchor='w')
- ut_button = tk.Button(topbar, bd=0, bg='white', relief='solid', text='Utilization', command=utilization)
- ut_button.pack(side='left', anchor='w')
- processes()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement