Advertisement
Techpad

Processes 10

Apr 3rd, 2021
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. import tkinter as tk
  2. import os
  3. from tkinter import ttk
  4. import time
  5. import sys
  6. from threading import Thread
  7. try:
  8. import psutil
  9. except ModuleNotFoundError:
  10. os.system("pip install psutil")
  11. import psutil
  12.  
  13. sys.setrecursionlimit(10**6)
  14.  
  15. _root = frame1
  16.  
  17. topbar = tk.Frame(_root, bd=0, bg='white')
  18. content = tk.Frame(_root, bd=0, bg='white')
  19.  
  20. topbar.pack(fill='x')
  21. content.pack(expand=True, fill='both')
  22.  
  23. currentscreen = "ut"
  24.  
  25. def utilization():
  26. global currentscreen
  27. global tk
  28. global ttk
  29. global content
  30. global time
  31. global Thread
  32. currentscreen = "ut"
  33. try:
  34. global refreshbutton
  35. refreshbutton.destroy()
  36. except:
  37. pass
  38. for child in content.winfo_children():
  39. child.destroy()
  40. cpucolor = "deepskyblue"
  41. memcolor = "purple1"
  42. dskcolor = "green3"
  43. sty = ttk.Style()
  44. sty.theme_use('winnative')
  45. sty.configure("cpu.Horizontal.TProgressbar", background=cpucolor, foreground=cpucolor, darkcolor=cpucolor, lightcolor=cpucolor, bordercolor=cpucolor, relief='solid')
  46. sty.configure("mem.Horizontal.TProgressbar", background=memcolor, foreground=memcolor, darkcolor=memcolor, lightcolor=memcolor, bordercolor=memcolor, relief='solid')
  47. sty.configure("dsk.Horizontal.TProgressbar", background=dskcolor, foreground=dskcolor, darkcolor=dskcolor, lightcolor=dskcolor, bordercolor=dskcolor, relief='solid')
  48. side1 = tk.Frame(content, bd=0, bg='white', padx=10)
  49. side2 = tk.Frame(content, bd=0, bg='white', padx=10)
  50. side1.pack(side="left", expand=True, fill="both")
  51. side2.pack(side="left", expand=True, fill="both")
  52. corner1 = tk.Frame(side1, bd=0, bg='white')
  53. corner2 = tk.Frame(side2, bd=0, bg='white')
  54. corner3 = tk.Frame(side2, bd=0, bg='white')
  55. corner1.pack(expand=True, fill="both")
  56. corner2.pack(expand=True, fill="both")
  57. corner3.pack(expand=True, fill="both")
  58. cpulabel = tk.Label(corner1, bg='white', text="CPU", font='TkDefaultFont 20')
  59. cpulabel.pack(anchor='nw')
  60. cpuamount = ttk.Progressbar(corner1, style="cpu.Horizontal.TProgressbar", orient="horizontal")
  61. cpuamount.pack(fill="x")
  62. cpupercentage = tk.Label(corner1, bg='white', text="0%")
  63. cpupercentage.pack(anchor='nw')
  64. memlabel = tk.Label(corner2, bg='white', text="Memory", font='TkDefaultFont 20')
  65. memlabel.pack(anchor='nw')
  66. memamount = ttk.Progressbar(corner2, style="mem.Horizontal.TProgressbar", orient="horizontal")
  67. memamount.pack(fill="x")
  68. mempercentage = tk.Label(corner2, bg='white', text="0% out of 0GB")
  69. mempercentage.pack(anchor='nw')
  70. dsklabel = tk.Label(corner3, bg='white', text="Disk", font='TkDefaultFont 20')
  71. dsklabel.pack(anchor='nw')
  72. dskamount = ttk.Progressbar(corner3, style="dsk.Horizontal.TProgressbar", orient="horizontal")
  73. dskamount.pack(fill="x")
  74. dsktotal = tk.Label(corner3, bg='white', text="0B")
  75. dsktotal.pack(anchor='nw')
  76. def update_ut():
  77. global currentscreen
  78. def humanbytes(B):
  79. """Return the given bytes as a human friendly KB, MB, GB, or TB string"""
  80. B = float(B)
  81. KB = float(1024)
  82. MB = float(KB ** 2) # 1,048,576
  83. GB = float(KB ** 3) # 1,073,741,824
  84. TB = float(KB ** 4) # 1,099,511,627,776
  85.  
  86. if B < KB:
  87. return '{0} {1}'.format(B, 'Bytes' if 0 == B > 1 else 'Byte')
  88. elif KB <= B < MB:
  89. return '{0:.2f} KB'.format(B / KB)
  90. elif MB <= B < GB:
  91. return '{0:.2f} MB'.format(B / MB)
  92. elif GB <= B < TB:
  93. return '{0:.2f} GB'.format(B / GB)
  94. elif TB <= B:
  95. return '{0:.2f} TB'.format(B / TB)
  96. while currentscreen == "ut":
  97. try:
  98. nonlocal cpuamount, cpupercentage, memamount, mempercentage, dsktotal, dskamount
  99. global psutil, time
  100. cpupercent = psutil.cpu_percent()
  101. vmem = psutil.virtual_memory()
  102. membytes = vmem.total
  103. totalmem = humanbytes(membytes)
  104. memused = humanbytes(vmem.used)
  105. mempercent = vmem.percent
  106. dsku = psutil.disk_usage("C:")
  107. dskbytes = dsku.total
  108. dskused = dsku.used
  109. dskpercent = dsku.percent
  110. totaldsk = humanbytes(dskbytes)
  111. useddsk = humanbytes(dskused)
  112. cpuamount.config(value=cpupercent)
  113. cpupercentage.config(text=f"{cpupercent}%")
  114. memamount.config(value=mempercent)
  115. mempercentage.config(text=f"{memused} out of {totalmem} ({mempercent}%)")
  116. dskamount.config(value=dskpercent)
  117. dsktotal.config(text=f"{useddsk} used of {totaldsk} ({dskpercent}%)")
  118. time.sleep(1)
  119. except:
  120. break
  121. Thread(target=update_ut, daemon=True).start()
  122.  
  123. def processes():
  124. global tk
  125. global psutil
  126. global content
  127. global topbar
  128. global Thread
  129. global currentscreen
  130. currentscreen = "pr"
  131. for child in content.winfo_children():
  132. child.destroy()
  133. canvas = tk.Canvas(content, bg='white', bd=0, highlightthickness=0)
  134. scrollbar = tk.Scrollbar(content, orient="vertical", command=canvas.yview, troughcolor='white', width=12)
  135. processlist = tk.Frame(canvas, bd=0, bg='white', highlightcolor='white', highlightthickness=0)
  136. processlist.bind(
  137. "<Configure>",
  138. lambda e: canvas.configure(
  139. scrollregion=canvas.bbox("all")
  140. )
  141. )
  142. canvas.create_window((0, 0), window=processlist, anchor="nw")
  143. canvas.configure(yscrollcommand=scrollbar.set)
  144.  
  145. def update_pr():
  146. import collections
  147. global time
  148. procs = []
  149. for proc in psutil.process_iter(['name']):
  150. procs.append(proc.info['name'])
  151. procs = sorted(procs, key=str.lower)
  152. procscount = collections.Counter(procs)
  153. for child in processlist.winfo_children():
  154. child.destroy()
  155. for i in procscount:
  156. if procscount[i] > 1:
  157. tk.Label(processlist, text=f"{i} ({str(procscount[i])})", bg='white', justify='left').pack(anchor='nw')
  158. else:
  159. tk.Label(processlist, text=i, bg='white', justify='left').pack(anchor='nw')
  160. scrollbar.update()
  161.  
  162. canvas.place(x=0, y=0, relwidth=1.0, relheight=1.0)
  163. scrollbar.pack(side="right", fill="y")
  164.  
  165. global refreshbutton
  166. refreshbutton = tk.Button(topbar, text='Refresh', bg='white', bd=0, command=update_pr)
  167. refreshbutton.pack(anchor='e')
  168.  
  169. update_pr()
  170.  
  171. pr_button = tk.Button(topbar, bd=0, bg='white', relief='solid', text='Processes', command=processes)
  172. pr_button.pack(side='left', anchor='w')
  173.  
  174. ut_button = tk.Button(topbar, bd=0, bg='white', relief='solid', text='Utilization', command=utilization)
  175. ut_button.pack(side='left', anchor='w')
  176.  
  177. processes()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement