Fundamentals 13 min read

Build a Windows‑Style Task Manager with Python, Tkinter, and psutil

This step‑by‑step guide shows how to create a Windows‑like Task Manager using Python's Tkinter GUI library and the psutil module, covering project setup, menu creation, button layout, real‑time system metrics, and code snippets for full implementation.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Build a Windows‑Style Task Manager with Python, Tkinter, and psutil

Introduction

In this tutorial we create a Windows‑style Task Manager using Python's Tkinter library and the psutil module to display processes, CPU usage, memory, network traffic, and user information.

Project Preparation

Editor: Sublime Text 3

Modules: psutil,

tkinter

Implementation Steps

1. Main Interface

We start by creating the main window and a menu bar.

m = t.Menu(root)

2. Menu Bar

We add File, Options, View, and Help sub‑menus.

# File menu
file = t.Menu(m, tearoff=False)
m.add_cascade(label='文件', menu=file)
file.add_command(label='新建任务', accelerator='(N)')
file.add_command(label='退出任务栏管理器', command=root.quit, accelerator='(x)')

# Options menu
ii = t.IntVar()
ii.set(1)
o = t.Menu(m, tearoff=False)
m.add_cascade(label='选项', menu=o)
o.add_radiobutton(label='前端显示', variable=ii, value=0)
o.add_radiobutton(label='使用时最小化', variable=ii, value=1)
o.add_radiobutton(label='最小化时隐藏', variable=ii, value=2)

# View menu
v = t.Menu(m, tearoff=False)
m.add_cascade(label='查看', menu=v)
v.add_command(label='立即刷新')
iv = t.IntVar()
iv.set(1)
s = t.Menu(v, tearoff=False)
v.add_cascade(label='更新速度', menu=s)
s.add_radiobutton(label='高', variable=iv, value=0)
s.add_radiobutton(label='普通', variable=iv, value=1)
s.add_radiobutton(label='低', variable=iv, value=2)
s.add_radiobutton(label='暂停', variable=iv, value=3)
v.add_command(label='选项列')

# Help menu
h = t.Menu(m, tearoff=False)
m.add_cascade(label='帮助', menu=h)
h.add_command(label='任务管理器帮助主体')
h.add_command(label='关于任务管理器')

Attach the menu to the root window:

root.configure(menu=m)

3. Buttons

Six buttons switch between different views.

b1 = t.Button(root, text='应用程序', command=yy)
b2 = t.Button(root, text='进程', command=jc)
b3 = t.Button(root, text='服务', command=fw)
b4 = t.Button(root, text='性能', command=xn)
b5 = t.Button(root, text='联网', command=lw)
b6 = t.Button(root, text='用户', command=yh)

b1.place(x=10, y=15, height=20, width=60)
b2.place(x=70, y=15, height=20, width=60)
b3.place(x=130, y=15, height=20, width=60)
b4.place(x=190, y=15, height=20, width=60)
b5.place(x=250, y=15, height=20, width=60)
b6.place(x=310, y=15, height=20, width=60)

4. Text Area and Scrollbar

A multi‑line Text widget displays output, with a vertical scrollbar.

text = t.Text(root, width=100, height=40)
text.place(x=10, y=36)

sb = t.Scrollbar(root)
sb.pack(side='left', fill='y')
sb.config(command=text.yview)
text.config(yscrollcommand=sb.set)

5. Status Labels

Three hidden labels will later show process count, CPU usage, and memory usage.

t1 = t.Label(text='')
t2 = t.Label(text='')
t3 = t.Label(text='')

6. Real‑Time Update Functions

Functions fetch system metrics using psutil and update the labels.

def jcs():
    t1.configure(text='进程数:' + str(len(psutil.pids())))
    root.after(3000, jcs)

def cpu():
    pp = str(ceil(psutil.cpu_percent(1)))
    t2.configure(text='CPU 使用率:' + pp + '%')
    root.after(1500, cpu)

def wlnc():
    f = psutil.virtual_memory().free
    t = psutil.virtual_memory().total
    wl = float(t - f) / float(t)
    t3.configure(text='物理内存:' + str(floor(wl * 100)) + '%')
    root.after(2000, wlnc)

7. Content Generation for Each Tab

Process tab lists PID, name, and executable path.

text.insert('insert', '进程号   进程名      进程文件路径
')
for y in psutil.pids():
    a = psutil.Process(y)
    if a.name() == 'System Idle Process':
        continue
    text.insert('insert', f"{y}     {a.name()}   {a.exe()}

")

Tasklist command (Windows) for a detailed process list:

mm = os.popen('tasklist')
text.insert('insert', mm.read())

Service information via sc query:

mm = os.popen('sc query type= service')
text.insert('insert', mm.read())

Performance tab shows boot time, current time, memory statistics, and swap usage.

l1 = t.Label(root, text='开机时间:')
boot = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
l2 = t.Label(root, text=str(boot))
# ... additional labels for current time, memory, swap ...
text.window_create('insert', window=l1)
text.window_create('insert', window=l2)
# ... insert other widgets similarly ...

Network tab displays received and sent traffic.

n = psutil.net_io_counters()
recv = str(float(n.bytes_recv) / 1024 / 1024) + 'MB'
sent = str(float(n.bytes_sent) / 1024 / 1024) + 'MB'
text.insert('insert', f"网卡接收流量: {recv}
网卡发送流量: {sent}
")

User tab lists current logged‑in users.

text.insert('insert', '    用户      状态
')
for u in psutil.users():
    text.insert('insert', f"{u.name}  运行中....
")

Summary

By combining Tkinter widgets with psutil, we built a functional task manager that shows processes, CPU load, memory usage, network traffic, and logged‑in users, providing a practical example of system monitoring in Python.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GUIPythonsystem-monitoringpsutilTkinterTask Manager
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.