实时网速显示_实例_python

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()
import psutil
import time

#获取网卡名称和其ip地址,不包括回环
def get_netcard(): 
	netcard_info = [] 
	info = psutil.net_if_addrs() 
	for k,v in info.items(): 
		for item in v:
			if item[0] == 2 and not item[1]=='127.0.0.1':  
				netcard_info.append((k,item[1])) 
	print(netcard_info)

get_netcard()
while True:
	s1 = psutil.net_io_counters(pernic=True)['本地连接']
	time.sleep(1)
	s2 = psutil.net_io_counters(pernic=True)['本地连接']
	result1 = s2.bytes_sent - s1.bytes_sent #上传
	result2 = s2.bytes_recv - s1.bytes_recv #下载

	#除法结果保留两位小数
	print(str('%.2f'%(result1 / 1024)) + ' kb/s',str('%.2f'%(result2 / 1024)) + ' kb/s')

 

你可能感兴趣的:(实时网速显示_实例_python)