之前写给女朋友的实时取色器功能太单一了,后来我更新成2.0版本,增添了一键复制的功能。最近女友需要同时拥有测距、RGB取色、十六进制取色的功能,更新后我命名为3.0版本,现分享一下 

需要用到的库: pyautogui、sys、ctypes、pyperclip、_thread、time

完整源码:
# -*- coding: utf-8 -*- import pyautogui, sys from ctypes import * #
获取屏幕上某个坐标的颜色 import pyperclip import _thread import time ccc = '' def
GET16(tmp): rgb = tmp.split(',') # 将RGB格式划分开来 strs = '#' for i in rgb: num =
int(i) # 将str转int # 将R、G、B分别转化为16进制拼接转换并大写 strs +=
str(hex(num))[-2:].replace('x', '0').upper() return strs def qiehuan(event):
global huan,ccc if huan == 'rgb': ccc = ' 十六进制模式' huan = 'get16' elif huan ==
'get16': ccc = ' 测距模式' huan = 'juli' elif huan == 'juli': ccc = ' RGB制模式' huan
= 'rgb' def GetColor(): r = 0 g = 0 b = 0 try: x, y = pyautogui.position()
gdi32 = windll.gdi32 user32 = windll.user32 hdc = user32.GetDC(None) # 获取颜色值
pixel = gdi32.GetPixel(hdc, x, y) # 提取RGB值 r = pixel & 0x0000ff g = (pixel &
0x00ff00) >> 8 b = pixel >> 16 except KeyboardInterrupt: print('\n') return r,
g, b def func(event): global ccc print(bb) pyperclip.copy(bb)
_thread.start_new_thread(tip, ()) def tip(): global ccc ccc = '复制成功,已复制到粘贴板'
time.sleep(1) ccc = '' def juli(event): global x1,x2,y1,y2,juli,ccc if juli
=='ling': x1, y1 = pyautogui.position() juli = 'yi' elif juli =='yi': juli =
'ling' elif juli =='er': ccc = '' juli ='ling' import tkinter as tk window =
tk.Tk() window.wm_attributes('-topmost', 1) window.title('小白取色器2.0')
window.geometry('310x100') window.bind("<Control-q>",func)
window.bind("<Control-w>",qiehuan) window.bind("<Control-e>",juli) huan = 'rgb'
juli = 'ling' a = tk.StringVar() cc = tk.StringVar() l = tk.Label(window,
textvariable=a, # 标签的文字 bg='green', # 标签背景颜色 font=('Arial', 12), # 字体和字体大小
width=15, height=2 # 标签长宽(以字符长度计算) ) l.pack() # 固定窗口位置 Label1 =
tk.Label(window, text='Ctrl+q复制/+w切换/+e拾取', font=('黑体', 12), fg = 'purple',
anchor='center').place(y=50,x=30) Label2 = tk.Label(window, textvariable=cc,
font=('黑体', 12), fg='red', anchor='center').place(y=75, x=30) l.focus_set()
try: while 1: global bb r, g, b = GetColor() a.set(str(r) + ',' + str(g) + ','
+ str(b)) bb = '{'+str(r)+','+str(g)+','+str(b)+'}' if huan == 'rgb':
a.set(str(r) + ',' + str(g) + ',' + str(b)) bb = '{' + str(r) + ',' + str(g) +
',' + str(b) + '}' elif huan =='get16': a.set(GET16(str(r) + ',' + str(g) + ','
+ str(b))) bb = GET16(str(r) + ',' + str(g) + ',' + str(b)) elif huan =='juli':
if juli =='ling': ccc = ' 请选择第一点' xx, yy = pyautogui.position() a.set('x=' +
str(xx) + ',y=' + str(yy)) if juli =='yi': ccc = ' 已选择第一点,请移动鼠标' xx, yy =
pyautogui.position() show_x = xx-x1 print('1') show_y = yy-y1
a.set(f'▲x={show_x},▲y={show_y}') print('1') cc.set(ccc) l.update() except:
print('已退出') window.mainloop()
窗体的实现: 
import tkinter as tk window = tk.Tk() window.wm_attributes('-topmost', 1)
window.title('小白取色器2.0') window.geometry('310x100')
window.bind("<Control-q>",func) window.bind("<Control-w>",qiehuan)
window.bind("<Control-e>",juli) huan = 'rgb' juli = 'ling' a = tk.StringVar()
cc = tk.StringVar() l = tk.Label(window, textvariable=a, # 标签的文字 bg='green', #
标签背景颜色 font=('Arial', 12), # 字体和字体大小 width=15, height=2 # 标签长宽(以字符长度计算) )
l.pack() # 固定窗口位置 Label1 = tk.Label(window, text='Ctrl+q复制/+w切换/+e拾取',
font=('黑体', 12), fg = 'purple', anchor='center').place(y=50,x=30) Label2 =
tk.Label(window, textvariable=cc, font=('黑体', 12), fg='red',
anchor='center').place(y=75, x=30) l.focus_set() try: while 1: global bb r, g,
b = GetColor() a.set(str(r) + ',' + str(g) + ',' + str(b)) bb =
'{'+str(r)+','+str(g)+','+str(b)+'}' if huan == 'rgb': a.set(str(r) + ',' +
str(g) + ',' + str(b)) bb = '{' + str(r) + ',' + str(g) + ',' + str(b) + '}'
elif huan =='get16': a.set(GET16(str(r) + ',' + str(g) + ',' + str(b))) bb =
GET16(str(r) + ',' + str(g) + ',' + str(b)) elif huan =='juli': if juli
=='ling': ccc = ' 请选择第一点' xx, yy = pyautogui.position() a.set('x=' + str(xx) +
',y=' + str(yy)) if juli =='yi': ccc = ' 已选择第一点,请移动鼠标' xx, yy =
pyautogui.position() show_x = xx-x1 print('1') show_y = yy-y1
a.set(f'▲x={show_x},▲y={show_y}') print('1') cc.set(ccc) l.update() except:
print('已退出') window.mainloop()
 

效果展示:

时间紧张,今日暂不细讲

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信