当前位置: 首页 > ai >正文

【Python】屏幕像素颜色值的获取

 一、一个小工具,按住F10键,颜色值会跟着显示。

import tkinter as tk
import pyautogui
import keyboardclass ColorViewer:def __init__(self):self.root = tk.Tk()self.root.overrideredirect(True)  # 无边框self.root.wm_attributes("-topmost", 1)  # 最前self.root.configure(bg="black")self.root.geometry("140x60")self.color_frame = tk.Frame(self.root, width=24, height=48, bg="white")self.color_frame.place(x=5, y=5)self.hex_label = tk.Label(self.root, text="#------", font=("Consolas", 13), bg="black", fg="white")self.hex_label.place(x=35, y=5)self.coord_label = tk.Label(self.root, text="(0000,0000)", font=("Consolas", 11), bg="black", fg="white")self.coord_label.place(x=35, y=30)self.update_loop()self.root.withdraw()  # 初始隐藏self.root.mainloop()def update_loop(self):if keyboard.is_pressed("F10"):x, y = pyautogui.position()r, g, b = pyautogui.screenshot(region=(x, y, 1, 1)).getpixel((0, 0))hex_color = "#{:02x}{:02x}{:02x}".format(r, g, b)self.color_frame.configure(bg=hex_color)self.hex_label.configure(text=hex_color)self.coord_label.configure(text=f"({x},{y})")# 自动移动窗口,避免遮挡鼠标screen_w = self.root.winfo_screenwidth()screen_h = self.root.winfo_screenheight()win_w, win_h = 140, 60offset = 20pos_x = x + offsetpos_y = y + offsetif pos_x + win_w > screen_w:pos_x = x - win_w - offsetif pos_y + win_h > screen_h:pos_y = y - win_h - offsetself.root.geometry(f"{win_w}x{win_h}+{pos_x}+{pos_y}")self.root.deiconify()else:self.root.withdraw()self.root.after(30, self.update_loop)  # 循环检查if __name__ == "__main__":ColorViewer()

二、样式示例

 

http://www.xdnf.cn/news/12658.html

相关文章:

  • uniapp 对接腾讯云IM群组成员管理(增删改查)
  • 14.MySQL使用C语言连接
  • 20、typedef和typename
  • 什么是异步 I/O?深入解析从基础到实践
  • 多区域协同的异地多活AI推理服务架构
  • 手机端抓包大麦网抢票协议:实现自动抢票与支付
  • 【C++进阶篇】C++11新特性(下篇)
  • 领域驱动设计(DDD)
  • 我计划做自己的小项目了
  • 多文化软件团队的协作之道:在认知差异中寻找协同的支点
  • BeckHoff(倍福) PLC 顺控器执行超时故障在北尔触摸屏显示的实现
  • CKA考试知识点分享(1)---HPA
  • 以智能管理为基础,楼宇自控打造建筑碳中和新路径
  • Day42 训练
  • 数据仓库建模的艺术论
  • 华为云Flexus+DeepSeek征文|华为云一键部署知识库搜索增强版Dify平台,构建智能聊天助手实战指南
  • 从标准输入直接执行 ELF 二进制文件的实用程序解析(C/C++实现)
  • ubuntu显示器未知
  • 深入理解 Agent 与 LLM 的区别:从智能体到语言模型
  • 【手动触发浏览器标签页图标自带转圈效果】
  • SQL-事务(2025.6.6-2025.6.7学习篇)
  • 如何思考?分析篇
  • 【Dv3Admin】系统视图下载中心API文件解析
  • 【Linux】Ubuntu 创建应用图标的方式汇总,deb/appimage/通用方法
  • 【HarmonyOS5】UIAbility组件生命周期详解:从创建到销毁的全景解析
  • 第3章:图数据模型与设计
  • Linux Gnome壁纸
  • 数据导入技术(文档加载)
  • Python 基础知识入门
  • Web设计之登录网页源码分享,PHP数据库连接,可一键运行!