Python PyQt5實(shí)戰(zhàn)項(xiàng)目之網(wǎng)速監(jiān)控器的實(shí)現(xiàn)
簡介
看到了一個(gè)能夠輕松實(shí)現(xiàn)獲取系統(tǒng)運(yùn)行的進(jìn)程和系統(tǒng)利用率(包括CPU、內(nèi)存、磁盤、網(wǎng)絡(luò)等)信息的模塊–psutil模塊。這次利用psutil.net_io_counters()這個(gè)方法。
psutil模塊使用
>>> psutil.net_io_counters() # 獲取網(wǎng)絡(luò)讀寫字節(jié)/包的個(gè)數(shù)
snetio(bytes_sent=16775953, bytes_recv=712657945, packets_sent=216741, packets_recv=485775, errin=0, errout=0, dropin=0, dropout=0)
bytes_sent:上傳數(shù)據(jù)
bytes_recv: 接收數(shù)據(jù)
主界面
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('網(wǎng)速')
self.resize(200,80)
self.setWindowOpacity(0.9) # 設(shè)置窗口透明度
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):
'''
重寫移動(dòng)事件
'''
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()
獲取鼠標(biāo)按下時(shí)的坐標(biāo)位置(相對于窗口左上角)
- mouseMoveEvent()
當(dāng)鼠標(biāo)處于按下狀態(tài)并開始移動(dòng)時(shí),鼠標(biāo)離窗口左上角的位置會(huì)不斷更新并保存在event.x()和event.y()中。
我們將更新后的x和y值不斷減去鼠標(biāo)按下時(shí)的坐標(biāo)位置,就可以知道鼠標(biāo)移動(dòng)的距離。最后再調(diào)用move方法將窗口當(dāng)前坐標(biāo)加上移動(dòng)距離即可
網(wǎng)速線程
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] #接收數(shù)據(jù)
send1 = parameter[0] #上傳數(shù)據(jù)
time.sleep(1) # 每隔1s監(jiān)聽端口接收數(shù)據(jù)
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('網(wǎng)速')
self.resize(200,80)
self.setWindowOpacity(0.9) # 設(shè)置窗口透明度
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):
'''
重寫移動(dòng)事件
'''
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] #接收數(shù)據(jù)
send1 = parameter[0] #上傳數(shù)據(jù)
time.sleep(1) # 每隔1s監(jiān)聽端口接收數(shù)據(jù)
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_())
成果展示


到此這篇關(guān)于Python PyQt5實(shí)戰(zhàn)項(xiàng)目之網(wǎng)速監(jiān)控器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python PyQt5 網(wǎng)速監(jiān)控器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 十個(gè)Python練手的實(shí)戰(zhàn)項(xiàng)目,學(xué)會(huì)這些Python就基本沒問題了(推薦)
- 分享7個(gè) Python 實(shí)戰(zhàn)項(xiàng)目練習(xí)
- Python實(shí)戰(zhàn)項(xiàng)目刮刮樂的實(shí)現(xiàn)詳解流程
- Python實(shí)戰(zhàn)項(xiàng)目之MySQL tkinter pyinstaller實(shí)現(xiàn)學(xué)生管理系統(tǒng)
- python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)爬取在行高手?jǐn)?shù)據(jù)
- python游戲?qū)崙?zhàn)項(xiàng)目之童年經(jīng)典超級瑪麗
- python游戲的魅力之冒險(xiǎn)島實(shí)戰(zhàn)項(xiàng)目
- Python實(shí)戰(zhàn)項(xiàng)目用PyQt5制作漫畫臉GUI界面
- python爬蟲實(shí)戰(zhàn)項(xiàng)目之爬取pixiv圖片
- 使用python來玩一次股票代碼詳解
相關(guān)文章
Python Handler處理器和自定義Opener原理詳解
這篇文章主要介紹了Python Handler處理器和自定義Opener原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Python實(shí)現(xiàn)的多項(xiàng)式擬合功能示例【基于matplotlib】
這篇文章主要介紹了Python實(shí)現(xiàn)的多項(xiàng)式擬合功能,結(jié)合實(shí)例形式分析了Python基于matplotlib模塊進(jìn)行數(shù)值運(yùn)算與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Python控制臺(tái)輸出時(shí)刷新當(dāng)前行內(nèi)容而不是輸出新行的實(shí)現(xiàn)
今天小編就為大家分享一篇Python控制臺(tái)輸出時(shí)刷新當(dāng)前行內(nèi)容而不是輸出新行的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python字典的概念及常見應(yīng)用實(shí)例詳解
這篇文章主要介紹了Python字典的概念及常見應(yīng)用,結(jié)合實(shí)例形式詳細(xì)的分析了Python字典的概念、原理、創(chuàng)建、常見操作函數(shù)與使用注意事項(xiàng),需要的朋友可以參考下2019-10-10
Python函數(shù)元數(shù)據(jù)實(shí)現(xiàn)為一個(gè)參數(shù)指定多個(gè)類型
這篇文章主要介紹了Python函數(shù)元數(shù)據(jù)實(shí)現(xiàn)為一個(gè)參數(shù)指定多個(gè)類型方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python實(shí)現(xiàn)Appium錄屏功能示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)Appium錄屏功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Python操控Chrome瀏覽器進(jìn)行網(wǎng)頁操作
這篇文章將為您展示如何通過Python控制瀏覽器實(shí)現(xiàn)網(wǎng)頁的打開、頁面的切換和關(guān)閉的基本操作,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-06-06
Python爬蟲使用瀏覽器cookies:browsercookie過程解析
這篇文章主要介紹了Python爬蟲使用瀏覽器cookies:browsercookie,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

