[Python] 实时PING网速显示器

835 0
Gr3yPh4ntom实名认证 2024-12-25 12:11:38 | 显示全部楼层 |阅读模式
[xmd]```
import tkinter as tk
import subprocess
import threading
import time
import ctypes


class NetworkMonitor:
    def __init__(self, root):
        self.root = root
        self.root.title("网络监测")
        
        # 设置窗口透明背景
        self.root.overrideredirect(True)  # 去掉窗口边框
        self.root.wm_attributes('-topmost', True)  # 窗口始终在最上层
        self.root.wm_attributes('-transparentcolor', 'white')  # 设置背景色为透明
        
        # 创建标签并设置字体大小
        self.label = tk.Label(root, text="正在检测网络...", font=("Arial", 14), bg='white')  # 背景色为白色
        self.label.pack(pady=20)

        # 更新网络状态
        self.update_network_status()

    def ping(self):
        try:
            # 使用 ping 命令来测试网络连通性
            output = subprocess.check_output("ping -c 1 chinesehongker.com", shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
            # 提取延迟信息并转换为整数
            if "超时" in output:
                on_no=1000
                return on_no
            latency = int(output.split("时间=")[1].split("ms")[0])
            return latency
        except Exception as e:
            return None

    def update_network_status(self):
        latency = self.ping()
        if latency is not None:
            if latency < 50:
                self.label.config(text=f"延迟: {latency} ms\n网络状态: 良好", fg="green")
            elif latency < 100:
                self.label.config(text=f"延迟: {latency} ms\n网络状态: 一般", fg="yellow")
            elif latency < 200:
                self.label.config(text=f"延迟: {latency} ms\n网络状态: 较差", fg="orange")
            elif latency == 1000:
                self.label.config(text="延迟: ∞ ms\n网络状态: 极差", fg="red")
            else:
                self.label.config(text=f"延迟: {latency} ms\n网络状态: 不佳", fg="red")
            
        else:
            pass

        # 每隔2秒更新一次
        self.root.after(2000, self.update_network_status)

if __name__ == "__main__":
    root = tk.Tk()
        #下面的注释部分有纰漏,暂时不用
    #告诉操作系统使用程序自身的dpi适配
    #ctypes.windll.shcore.SetProcessDpiAwareness(1)
    #获取屏幕的缩放因子
    #ScaleFactor=ctypes.windll.shcore.GetScaleFactorForDevice(0)
    #设置程序缩放
    #root.tk.call('tk', 'scaling', ScaleFactor/75)
    root.geometry("300x100+{0}+{1}".format(root.winfo_screenwidth() - 300, root.winfo_screenheight() - 100))  # 右下角
    # 设置窗口透明
    root.config(bg='white')  # 窗口背景设为白色(将被透明化)
   

   
    app = NetworkMonitor(root)
    root.mainloop()
```
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Gr3yPh4ntom

高级红客

关注
  • 57
    主题
  • 1
    粉丝
  • 5
    关注
黑客界的耻辱 软件开发圈的耻辱 绘圈的耻辱 vocaloid圈的耻辱

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行