找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 92|回复: 2

[辅助教学] 桌面编号程序

[复制链接]
发表于 昨天 08:15 | 显示全部楼层 |阅读模式
本帖最后由 515151 于 2026-4-17 08:17 编辑

桌面编号程序 2026-04-17_081628.jpg
host_info_transparent.rar (6.23 MB, 下载次数: 69)
  1. import socket
  2. import tkinter as tk
  3. from tkinter import font
  4. import sys
  5. import winreg
  6. def get_host_info():
  7.     """获取主机名和 IP 地址"""
  8.     hostname = socket.gethostname()
  9.     try:
  10.         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  11.         s.connect(("8.8.8.8", 80))
  12.         ip_address = s.getsockname()[0]
  13.         s.close()
  14.     except Exception:
  15.         ip_address = socket.gethostbyname(hostname)
  16.     return hostname, ip_address
  17. def is_system_dark_mode():
  18.     """检测 Windows 系统是否启用深色模式"""
  19.     try:
  20.         if sys.platform.startswith('win'):
  21.             key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
  22.                                 r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize")
  23.             value, _ = winreg.QueryValueEx(key, "AppsUseLightTheme")
  24.             winreg.CloseKey(key)
  25.             return value == 0
  26.     except:
  27.         pass
  28.     return True
  29. def get_text_colors():
  30.     """根据系统主题获取文字颜色"""
  31.     dark_mode = is_system_dark_mode()
  32.     if dark_mode:
  33.         host_color = "#00FF99"
  34.         ip_color = "#00DDFF"
  35.     else:
  36.         host_color = "#005533"
  37.         ip_color = "#0066AA"
  38.     return host_color, ip_color
  39. def create_display_window():
  40.     """创建桌面显示窗口 - 只显示主机名和IP,靠右对齐"""
  41.     hostname, ip_address = get_host_info()
  42.    
  43.     root = tk.Tk()
  44.     root.title("主机信息")
  45.    
  46.     screen_width = root.winfo_screenwidth()
  47.    
  48.     # 足够宽确保长文字也能全部显示
  49.     window_width = 620
  50.     window_height = 130
  51.    
  52.     # 靠右上角定位
  53.     x_position = screen_width - window_width - 10
  54.     y_position = 10
  55.    
  56.     root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")
  57.    
  58.     # 窗口属性:无边框、始终置顶
  59.     root.overrideredirect(True)
  60.     root.attributes("-topmost", True)
  61.    
  62.     # 设置完全透明背景
  63.     transparent_color = "gray15"
  64.     root.configure(bg=transparent_color)
  65.     root.wm_attributes('-transparentcolor', transparent_color)
  66.    
  67.     host_color, ip_color = get_text_colors()
  68.    
  69.     # 20号字体
  70.     title_font = font.Font(family="Microsoft YaHei", size=20, weight="bold")
  71.     content_font = font.Font(family="Microsoft YaHei", size=20)
  72.    
  73.     # 主框架背景透明,右侧对齐
  74.     frame = tk.Frame(root, bg=transparent_color)
  75.     frame.pack(expand=True, fill="both")
  76.    
  77.     # 只显示主机名,靠右对齐
  78.     label_host = tk.Label(
  79.         frame,
  80.         text=hostname,
  81.         font=title_font,
  82.         fg=host_color,
  83.         bg=transparent_color,
  84.         anchor="e"
  85.     )
  86.     label_host.pack(fill="x", padx=10, pady=(18, 8))
  87.    
  88.     # 只显示IP地址,靠右对齐
  89.     label_ip = tk.Label(
  90.         frame,
  91.         text=ip_address,
  92.         font=content_font,
  93.         fg=ip_color,
  94.         bg=transparent_color,
  95.         anchor="e"
  96.     )
  97.     label_ip.pack(fill="x", padx=10, pady=(8, 18))
  98.    
  99.     # 双击关闭
  100.     def close_window(event=None):
  101.         root.destroy()
  102.    
  103.     root.bind("<Double-Button-1>", close_window)
  104.    
  105.     root.mainloop()
  106. if __name__ == "__main__":
  107.     create_display_window()
复制代码


回复

使用道具 举报

发表于 昨天 16:19 | 显示全部楼层
以前好像有人发过
回复

使用道具 举报

发表于 昨天 23:28 | 显示全部楼层
win7 运行报错
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|腾飞在线 ( 冀ICP备05002530号 )

GMT+8, 2026-4-18 18:33 , Processed in 0.074621 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表