简介
看到了一个能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息的模块–psutil模块。这次利用psutil.net_io_counters()这个方法。
psutil模块使用
>>> psutil.net_io_counters() # 获取网络读写字节/包的个数
snetio(bytes_sent=16775953, bytes_recv=712657945, packets_sent=216741, packets_recv=485775, errin=0, errout=0, dropin=0, dropout=0)
bytes_sent:上传数据
bytes_recv: 接收数据
主界面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
class NetWindows(QMainWindow): net_signal = pyqtSignal( str , str ) def __init__( self ): super (NetWindows, self ).__init__() self .ui_init() self .thread_init() def ui_init( self ): self .setWindowTitle( '网速' ) self .resize( 200 , 80 ) self .setWindowOpacity( 0.9 ) # 设置窗口透明度 self .setWindowFlag(Qt.FramelessWindowHint) # 隐藏边框 self .setWindowFlag(Qt.WindowStaysOnTopHint) # 窗口始终显示在最前面 self .upload_icon = QLabel() self .upload_icon.setPixmap(QPixmap( ':res/upload.png' )) self .upload_icon.setScaledContents( True ) self .download_icon = QLabel() self .download_icon.setPixmap(QPixmap( ':res/download.png' )) self .download_icon.setScaledContents( True ) self .upload_text = QLabel() self .upload_text.setText( 'upload: ' ) self .download_text = QLabel() self .download_text.setText( 'download: ' ) self .upload_lab = QLabel() self .download_lab = QLabel() self .g_layout = QGridLayout() self .g_layout.addWidget( self .upload_icon, 0 , 0 , 1 , 1 ) self .g_layout.addWidget( self .download_icon, 1 , 0 , 1 , 1 ) self .g_layout.addWidget( self .upload_text, 0 , 1 , 1 , 1 ) self .g_layout.addWidget( self .download_text, 1 , 1 , 1 , 1 ) self .g_layout.addWidget( self .upload_lab, 0 , 2 , 1 , 4 ) self .g_layout.addWidget( self .download_lab, 1 , 2 , 1 , 4 ) self .widget = QWidget() self .widget.setLayout( self .g_layout) self .setCentralWidget( self .widget) def thread_init( self ): self .net_thread = NetThread() self .net_thread.net_signal.connect( self .net_slot) self .net_thread.start( 1000 ) def variate_init( self ): self .upload_content = '' self .download_content = '' def net_slot( self ,upload_content,download_content): self .upload_lab.setText(upload_content) self .download_lab.setText(download_content) def mousePressEvent( self , event): ''' 重写按下事件 ''' self .start_x = event.x() self .start_y = event.y() def mouseMoveEvent( self , event): ''' 重写移动事件 ''' dis_x = event.x() - self .start_x dis_y = event.y() - self .start_y self .move( self .x() + dis_x, self .y() + dis_y) |
- mousePressEvent()
获取鼠标按下时的坐标位置(相对于窗口左上角)
- mouseMoveEvent()
当鼠标处于按下状态并开始移动时,鼠标离窗口左上角的位置会不断更新并保存在event.x()和event.y()中。
我们将更新后的x和y值不断减去鼠标按下时的坐标位置,就可以知道鼠标移动的距离。最后再调用move方法将窗口当前坐标加上移动距离即可
网速线程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class NetThread(QThread): net_signal = pyqtSignal( str , str ) def __init__( self ): super (NetThread, self ).__init__() def net_func( self ): parameter = psutil.net_io_counters() recv1 = parameter[ 1 ] #接收数据 send1 = parameter[ 0 ] #上传数据 time.sleep( 1 ) # 每隔1s监听端口接收数据 parameter = psutil.net_io_counters() recv2 = parameter[ 1 ] send2 = parameter[ 0 ] self .upload_content = '{:.1f} kb/s.' . format ((send2 - send1) / 1024.0 ) self .download_content = '{:.1f} kb/s.' . format ((recv2 - recv1) / 1024.0 ) def run( self ): while ( 1 ): self .net_func() self .net_signal.emit( self .upload_content, self .download_content) time.sleep( 1 ) |
全部代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
import sys import time import psutil from PyQt5.QtWidgets import QApplication, QHBoxLayout, QMainWindow, QWidget, QFrame, QLabel, QVBoxLayout, QGridLayout from PyQt5.QtCore import Qt, pyqtSignal, QThread from PyQt5.QtGui import QPixmap import res class NetWindows(QMainWindow): net_signal = pyqtSignal( str , str ) def __init__( self ): super (NetWindows, self ).__init__() self .ui_init() self .thread_init() def ui_init( self ): self .setWindowTitle( '网速' ) self .resize( 200 , 80 ) self .setWindowOpacity( 0.9 ) # 设置窗口透明度 self .setWindowFlag(Qt.FramelessWindowHint) # 隐藏边框 self .setWindowFlag(Qt.WindowStaysOnTopHint) # 窗口始终显示在最前面 self .upload_icon = QLabel() self .upload_icon.setPixmap(QPixmap( ':res/upload.png' )) self .upload_icon.setScaledContents( True ) self .download_icon = QLabel() self .download_icon.setPixmap(QPixmap( ':res/download.png' )) self .download_icon.setScaledContents( True ) self .upload_text = QLabel() self .upload_text.setText( 'upload: ' ) self .download_text = QLabel() self .download_text.setText( 'download: ' ) self .upload_lab = QLabel() self .download_lab = QLabel() self .g_layout = QGridLayout() self .g_layout.addWidget( self .upload_icon, 0 , 0 , 1 , 1 ) self .g_layout.addWidget( self .download_icon, 1 , 0 , 1 , 1 ) self .g_layout.addWidget( self .upload_text, 0 , 1 , 1 , 1 ) self .g_layout.addWidget( self .download_text, 1 , 1 , 1 , 1 ) self .g_layout.addWidget( self .upload_lab, 0 , 2 , 1 , 4 ) self .g_layout.addWidget( self .download_lab, 1 , 2 , 1 , 4 ) self .widget = QWidget() self .widget.setLayout( self .g_layout) self .setCentralWidget( self .widget) def thread_init( self ): self .net_thread = NetThread() self .net_thread.net_signal.connect( self .net_slot) self .net_thread.start( 1000 ) def variate_init( self ): self .upload_content = '' self .download_content = '' def net_slot( self ,upload_content,download_content): self .upload_lab.setText(upload_content) self .download_lab.setText(download_content) def mousePressEvent( self , event): ''' 重写按下事件 ''' self .start_x = event.x() self .start_y = event.y() def mouseMoveEvent( self , event): ''' 重写移动事件 ''' dis_x = event.x() - self .start_x dis_y = event.y() - self .start_y self .move( self .x() + dis_x, self .y() + dis_y) class NetThread(QThread): net_signal = pyqtSignal( str , str ) def __init__( self ): super (NetThread, self ).__init__() def net_func( self ): parameter = psutil.net_io_counters() recv1 = parameter[ 1 ] #接收数据 send1 = parameter[ 0 ] #上传数据 time.sleep( 1 ) # 每隔1s监听端口接收数据 parameter = psutil.net_io_counters() recv2 = parameter[ 1 ] send2 = parameter[ 0 ] self .upload_content = '{:.1f} kb/s.' . format ((send2 - send1) / 1024.0 ) self .download_content = '{:.1f} kb/s.' . format ((recv2 - recv1) / 1024.0 ) def run( self ): while ( 1 ): self .net_func() self .net_signal.emit( self .upload_content, self .download_content) time.sleep( 1 ) if __name__ = = '__main__' : app = QApplication(sys.argv) dispaly = NetWindows() dispaly.show() netwidows = NetWindows() sys.exit(app.exec_()) |
成果展示
到此这篇关于Python PyQt5实战项目之网速监控器的实现的文章就介绍到这了,更多相关Python PyQt5 网速监控器内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/m0_46778548/article/details/115769459