之前寫給女朋友的實時取色器功能太單一了,後來我更新成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()
效果展示:
時間緊張,今日暫不細講