时间:2022-09-23 10:10:25 | 栏目:Python代码 | 点击:次
看到了一个能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息的模块–psutil模块。这次利用psutil.net_io_counters()这个方法。
>>> 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)
获取鼠标按下时的坐标位置(相对于窗口左上角)
当鼠标处于按下状态并开始移动时,鼠标离窗口左上角的位置会不断更新并保存在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_())