Python PyQt5实战项目之网速监控器的实现
作者:不侠居 时间:2023-05-25 12:57:07
简介
看到了一个能够轻松实现获取系统运行的进程和系统利用率(包括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: 接收数据
主界面
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方法将窗口当前坐标加上移动距离即可
网速线程
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)
全部代码
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_())
成果展示
来源:https://blog.csdn.net/m0_46778548/article/details/115769459
标签:Python,PyQt5,网速监控器
0
投稿
猜你喜欢
vue组件定义,全局、局部组件,配合模板及动态组件功能示例
2024-05-05 09:09:59
ASP开发的WAP格式简易邮件系统实例
2008-06-10 17:00:00
解决python selenium3启动不了firefox的问题
2022-12-02 11:36:34
Python3.5实现的三级菜单功能示例
2023-08-01 13:37:26
mysql drop database删除数据库命令实例讲解
2024-01-18 19:40:01
Python3利用Dlib实现摄像头实时人脸检测和平铺显示示例
2021-12-14 16:37:30
如何做好一份前端工程师的简历?
2009-02-17 12:52:00
基于scrapy的redis安装和配置方法
2022-07-15 17:26:56
js+css实现select的美化效果
2024-04-16 09:52:25
AJAX请求类下载
2007-08-23 08:33:00
长期使用中型Access数据库的一点经验
2007-12-21 13:23:00
JavaScript中call,apply,bind的区别与实现
2024-04-22 12:51:26
Python实现单项链表的最全教程
2021-12-21 09:45:26
Centos7.3下mysql5.7.18安装并修改初始密码的方法
2024-01-20 00:02:11
Git在Windows中安装与使用教程
2023-11-01 08:47:55
基于PHP实现解密或加密Cloudflar邮箱保护
2023-07-22 13:22:37
TypeScript学习之强制类型的转换
2024-04-25 13:08:07
Python虚拟机栈帧对象及获取源码学习
2022-08-26 07:15:30
关于Python卷积函数详解
2023-06-14 01:09:27
Python处理XML格式数据的方法详解
2021-04-10 22:25:07