欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python PyQt5實(shí)戰(zhàn)項(xiàng)目之網(wǎng)速監(jiān)控器的實(shí)現(xiàn)

 更新時(shí)間:2021年11月09日 10:06:59   作者:不俠居  
PyQt5以一套Python模塊的形式來(lái)實(shí)現(xiàn)功能。它包含了超過(guò)620個(gè)類,600個(gè)方法和函數(shù)。它是一個(gè)多平臺(tái)的工具套件,它可以運(yùn)行在所有的主流操作系統(tǒng)中,包含Unix,Windows和Mac OS。PyQt5采用雙重許可模式。開(kāi)發(fā)者可以在GPL和社區(qū)授權(quán)之間選擇

簡(jiǎn)介

看到了一個(gè)能夠輕松實(shí)現(xiàn)獲取系統(tǒng)運(yùn)行的進(jìn)程和系統(tǒng)利用率(包括CPU、內(nèi)存、磁盤(pán)、網(wǎng)絡(luò)等)信息的模塊–psutil模塊。這次利用psutil.net_io_counters()這個(gè)方法。

psutil模塊使用

>>> psutil.net_io_counters() # 獲取網(wǎng)絡(luò)讀寫(xiě)字節(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):
        '''
        重寫(xiě)按下事件
        '''                             
        self.start_x = event.x()                        
        self.start_y = event.y()

    def mouseMoveEvent(self, event):
        '''
        重寫(xiě)移動(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)位置(相對(duì)于窗口左上角)

  • mouseMoveEvent()

當(dāng)鼠標(biāo)處于按下?tīng)顟B(tài)并開(kā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)聽(tīng)端口接收數(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):
        '''
        重寫(xiě)按下事件
        '''                             
        self.start_x = event.x()                        
        self.start_y = event.y()

    def mouseMoveEvent(self, event):
        '''
        重寫(xiě)移動(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)聽(tīng)端口接收數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python PyQt5學(xué)習(xí)之樣式設(shè)置詳解

    Python PyQt5學(xué)習(xí)之樣式設(shè)置詳解

    這篇文章主要為大家詳細(xì)介紹了Python PyQt5中樣式設(shè)置的相關(guān)資料,例如為標(biāo)簽添加背景圖片、為按鈕添加背景圖片、設(shè)置窗口透明等,感興趣的可以學(xué)習(xí)一下
    2022-12-12
  • Python打造出適合自己的定制化Eclipse IDE

    Python打造出適合自己的定制化Eclipse IDE

    這篇文章主要介紹了Python打造出適合自己的定制化Eclipse IDE的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • python中列表的切片與修改知識(shí)點(diǎn)總結(jié)

    python中列表的切片與修改知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家分享了關(guān)于python中列表的切片與修改的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-07-07
  • Python 常用模塊 re 使用方法詳解

    Python 常用模塊 re 使用方法詳解

    這篇文章主要介紹了Python 常用模塊 re 使用方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • python中def是做什么的

    python中def是做什么的

    在本篇文章里小編給大家分享的是關(guān)于python中def的作用以及相關(guān)用法,有需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • python3音樂(lè)播放器簡(jiǎn)單實(shí)現(xiàn)代碼

    python3音樂(lè)播放器簡(jiǎn)單實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了python3音樂(lè)播放器簡(jiǎn)單實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 對(duì)pandas數(shù)據(jù)判斷是否為NaN值的方法詳解

    對(duì)pandas數(shù)據(jù)判斷是否為NaN值的方法詳解

    今天小編就為大家分享一篇對(duì)pandas數(shù)據(jù)判斷是否為NaN值的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • Django零基礎(chǔ)入門(mén)之模板變量詳解

    Django零基礎(chǔ)入門(mén)之模板變量詳解

    這篇文章主要介紹了Django零基礎(chǔ)入門(mén)之模板變量詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • python實(shí)現(xiàn)屏保程序(適用于背單詞)

    python實(shí)現(xiàn)屏保程序(適用于背單詞)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)屏保程序,適用于背單詞,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • python制作簡(jiǎn)單計(jì)算器功能

    python制作簡(jiǎn)單計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了python制作簡(jiǎn)單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評(píng)論