python 实时取色器3.0(升级版)【内附源码】
生活随笔
收集整理的這篇文章主要介紹了
python 实时取色器3.0(升级版)【内附源码】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前寫給女朋友的實時取色器功能太單一了,后來我更新成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 strsdef qiehuan(event):global huan,cccif huan == 'rgb':ccc = ' 十六進制模式'huan = 'get16'elif huan == 'get16':ccc = ' 測距模式'huan = 'juli'elif huan == 'juli':ccc = ' RGB制模式'huan = 'rgb'def GetColor():r = 0g = 0b = 0try:x, y = pyautogui.position()gdi32 = windll.gdi32user32 = windll.user32hdc = user32.GetDC(None) # 獲取顏色值pixel = gdi32.GetPixel(hdc, x, y) # 提取RGB值r = pixel & 0x0000ffg = (pixel & 0x00ff00) >> 8b = pixel >> 16except KeyboardInterrupt:print('\n')return r, g, bdef func(event):global cccprint(bb)pyperclip.copy(bb)_thread.start_new_thread(tip, ()) def tip():global cccccc = '復制成功,已復制到粘貼板'time.sleep(1)ccc = ''def juli(event):global x1,x2,y1,y2,juli,cccif juli =='ling':x1, y1 = pyautogui.position()juli = 'yi'elif juli =='yi':juli = 'ling'elif juli =='er':ccc = ''juli ='ling'import tkinter as tkwindow = 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 bbr, 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-x1print('1')show_y = yy-y1a.set(f'▲x={show_x},▲y={show_y}')print('1')cc.set(ccc)l.update() except:print('已退出') window.mainloop()窗體的實現:?
import tkinter as tkwindow = 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 bbr, 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-x1print('1')show_y = yy-y1a.set(f'▲x={show_x},▲y={show_y}')print('1')cc.set(ccc)l.update() except:print('已退出') window.mainloop()?
效果展示:
時間緊張,今日暫不細講
總結
以上是生活随笔為你收集整理的python 实时取色器3.0(升级版)【内附源码】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: link引入html5,CSS引入方式
- 下一篇: python 坦克大战游戏