美文网首页PythonPythonPython
python:实时网速监控小工具

python:实时网速监控小工具

作者: 獨孤記憶 | 来源:发表于2018-08-14 17:43 被阅读174次
    1212.gif
    import psutil
    import time
    from tkinter import *
    
    def make_app():
        app =Tk()
        app.geometry('200x100')
        app.config(bg='#303030')
        Label(text='实时网速监控',font=('Hack',23,'bold'),bg='#303030',fg='white').pack()
        Label(name='lb2',text='_kb/s'     ,font=('Hack',20,'bold'),bg='#303030',fg='white').pack()
        return app
    
    def speed_test():
    
        s1 = psutil.net_io_counters(pernic=True)['以太网']
        time.sleep(1)
        s2 = psutil.net_io_counters(pernic=True)['以太网']
        result = s2.bytes_recv - s1.bytes_recv
        #除法结果保留两位小数
        return str('%.2f'%(result / 1024)) + 'kb/s'
    
    def ui_updata(do):
        data = do()
        #app下名字是lb2的子控件
        lb2  = app.children['lb2']
        #配置,替换原来的text
        lb2.config(text=data)
        #每1秒后调用lambda:ui_updata(do)函数
        app.after(1000,lambda:ui_updata(do))
    
    app = make_app()
    #每1秒后调用ui_updata(speed_test)函数
    app.after(1000,lambda :ui_updata(speed_test))
    app.mainloop()

    相关文章

      网友评论

      • 王诗翔:很有意思,不过我运行报错了,是软件版本问题吗?

        app.mainloop()
        Exception in Tkinter callback
        Traceback (most recent call last):
        File "/Volumes/Data/miniconda3/lib/python3.6/tkinter/__init__.py", line 1702, in __call__
        return self.func(*args)
        File "/Volumes/Data/miniconda3/lib/python3.6/tkinter/__init__.py", line 746, in callit
        func(*args)
        File "<ipython-input-10-18e3ebb21af1>", line 1, in <lambda>
        app.after(1000, lambda: ui_updata(speed_test))
        File "<ipython-input-8-306704cff85f>", line 2, in ui_updata
        data = do()
        File "<ipython-input-7-7e7a2f6076ed>", line 3, in speed_test
        s1 = psutil.net_io_counters(pernic=True)['以太网']

      本文标题:python:实时网速监控小工具

      本文链接:https://www.haomeiwen.com/subject/ccnvbftx.html