桌面编号程序
本帖最后由 515151 于 2026-4-17 08:17 编辑桌面编号程序
import socket
import tkinter as tk
from tkinter import font
import sys
import winreg
def get_host_info():
"""获取主机名和 IP 地址"""
hostname = socket.gethostname()
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip_address = s.getsockname()
s.close()
except Exception:
ip_address = socket.gethostbyname(hostname)
return hostname, ip_address
def is_system_dark_mode():
"""检测 Windows 系统是否启用深色模式"""
try:
if sys.platform.startswith('win'):
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize")
value, _ = winreg.QueryValueEx(key, "AppsUseLightTheme")
winreg.CloseKey(key)
return value == 0
except:
pass
return True
def get_text_colors():
"""根据系统主题获取文字颜色"""
dark_mode = is_system_dark_mode()
if dark_mode:
host_color = "#00FF99"
ip_color = "#00DDFF"
else:
host_color = "#005533"
ip_color = "#0066AA"
return host_color, ip_color
def create_display_window():
"""创建桌面显示窗口 - 只显示主机名和IP,靠右对齐"""
hostname, ip_address = get_host_info()
root = tk.Tk()
root.title("主机信息")
screen_width = root.winfo_screenwidth()
# 足够宽确保长文字也能全部显示
window_width = 620
window_height = 130
# 靠右上角定位
x_position = screen_width - window_width - 10
y_position = 10
root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")
# 窗口属性:无边框、始终置顶
root.overrideredirect(True)
root.attributes("-topmost", True)
# 设置完全透明背景
transparent_color = "gray15"
root.configure(bg=transparent_color)
root.wm_attributes('-transparentcolor', transparent_color)
host_color, ip_color = get_text_colors()
# 20号字体
title_font = font.Font(family="Microsoft YaHei", size=20, weight="bold")
content_font = font.Font(family="Microsoft YaHei", size=20)
# 主框架背景透明,右侧对齐
frame = tk.Frame(root, bg=transparent_color)
frame.pack(expand=True, fill="both")
# 只显示主机名,靠右对齐
label_host = tk.Label(
frame,
text=hostname,
font=title_font,
fg=host_color,
bg=transparent_color,
anchor="e"
)
label_host.pack(fill="x", padx=10, pady=(18, 8))
# 只显示IP地址,靠右对齐
label_ip = tk.Label(
frame,
text=ip_address,
font=content_font,
fg=ip_color,
bg=transparent_color,
anchor="e"
)
label_ip.pack(fill="x", padx=10, pady=(8, 18))
# 双击关闭
def close_window(event=None):
root.destroy()
root.bind("<Double-Button-1>", close_window)
root.mainloop()
if __name__ == "__main__":
create_display_window()
以前好像有人发过 win7 运行报错 谢谢分享,win10运行没有问题:lol :):):)改了参数还是一样的。 :):):)改了参数还是一样的。
页:
[1]