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

【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()

二、样式示例

 

相关文章:

  • SpringBoot-17-MyBatis动态SQL标签之常用标签
  • Unity的日志管理类
  • 【CF】Day78——⭐Codeforces Round 876 (Div. 2) D (LIS | 思维 | DP)
  • 15-Oracle 23ai Vector Search Similarity Search-向量相似性和混合搜索-实操
  • go工具库:hertz api框架 hertz client的使用
  • 【数据结构】详解算法复杂度:时间复杂度和空间复杂度
  • 使用python实现奔跑的线条效果
  • TTL简述
  • 基于算法竞赛的c++编程(20)函数的递归
  • OpenLayers:封装Tooltip
  • stm32—ADC和DAC
  • Linux操作系统故障应急场景及对应排查方法
  • 湖北理元理律师事务所:债务优化中的民生保障实践
  • FreeRTOS任务之深入篇
  • 关键字--sizeof
  • Python抽象基类实战:构建广告轮播框架ADAM的核心逻辑
  • robot_lab train的整体逻辑
  • SDC命令详解:使用set_propagated_clock命令进行约束
  • 如何思考?分析篇
  • 深入剖析MySQL锁机制,多事务并发场景锁竞争
  • 中国建设银行吉林分行网站/seo关键词优化指南
  • 真做视频网站/嘉峪关seo
  • wordpress商城 中文站/发稿
  • 外包app公司不给源代码/长沙seo智优营家
  • 如何自己做网站发布到服务器上面/福建百度代理公司
  • 郑州网站建设市场/永久观看不收费的直播