使用PyQt5編寫一個簡單的取色器
PyQt5搭建的一個取色器,一共寫了兩款應(yīng)用,一款使用快捷鍵捕獲鼠標附近圖像的RGB和16進制顏色編碼,一款跟隨鼠標刷新圖像的RGB和16進制顏色編碼。桌面應(yīng)用程序的背景色切換也可以參考此程序。
源程序的git地址: gitee.com/mtoooo/color_picker
打包的exe下載鏈接: gitee.com/mtoooo/color_picker
取色器1
源代碼參考main.py,也可以點擊頂部exe鏈接下載取色器.exe文件直接使用,取色快捷鍵Shift+A,應(yīng)用程序會顯示RGB和16進制顏色編碼。
源程序初始化
pip install PyQt5==5.15.10
程序啟動
python main.py
取色快捷鍵Shift+A
交互效果

main.py
import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QColor, QPixmap, QCursor, QPainter, QPen, QBrush
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout
class ColorPickerApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Color Picker")
self.setGeometry(100, 100, 300, 200)
# 創(chuàng)建標簽,用于顯示 RGB 和 16 進制顏色
self.rgb_hex_label = QLabel("RGB: None\nHex: None", self)
# 設(shè)置標簽樣式和大小
self.rgb_hex_label.setFixedSize(150, 50) # 設(shè)置固定大小 100x50
self.rgb_hex_label.setStyleSheet("font-size: 12px; padding: 5px; border-radius: 5px; background-color: white; border: 1px solid black;")
# 設(shè)置布局,使用 QHBoxLayout 和 QVBoxLayout 居中顯示標簽
layout = QVBoxLayout(self)
layout.setAlignment(Qt.AlignCenter) # 設(shè)置垂直布局居中
layout.addWidget(self.rgb_hex_label)
self.setLayout(layout)
# 定時器用于定時獲取顏色信息
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_color)
self.timer.start(100) # 每100毫秒更新一次
# 用來存儲背景顏色
self.bg_color = QColor(255, 255, 255) # 默認背景為白色
def update_color(self):
# 獲取鼠標位置
cursor_pos = QCursor.pos()
# 獲取屏幕截圖并獲取當前鼠標位置的顏色
screen = QApplication.primaryScreen()
pixmap = screen.grabWindow(0)
color = QColor(pixmap.toImage().pixel(cursor_pos))
# 獲取 RGB 和 16 進制顏色值
rgb = color.getRgb()
hex_color = color.name()
# 更新標簽顯示顏色信息
self.rgb_hex_label.setText(f"RGB: {rgb[0]}, {rgb[1]}, {rgb[2]}\nHex: {hex_color}")
# 改變窗口背景色
self.bg_color = color
# 刷新窗口
self.update()
def paintEvent(self, event):
# 繪制背景顏色
painter = QPainter(self)
painter.setBrush(QBrush(self.bg_color))
painter.setPen(Qt.NoPen)
painter.drawRect(self.rect()) # 填充整個窗口背景
painter.end()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ColorPickerApp()
window.show()
sys.exit(app.exec_())
取色器2
源代碼參考main2.py, 也可以下載取色器2.exe文件直接使用,無需快捷鍵運行即可使用,應(yīng)用程序會顯示RGB和16進制顏色編碼。
- 源程序初始化
pip install PyQt5==5.15.10
程序啟動
python main2.py
交互效果

main2.py
import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QColor, QPixmap, QCursor, QPainter, QPen, QBrush
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout
class ColorPickerApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Color Picker")
self.setGeometry(100, 100, 300, 200)
# 創(chuàng)建標簽,用于顯示 RGB 和 16 進制顏色
self.rgb_hex_label = QLabel("RGB: None\nHex: None", self)
# 設(shè)置標簽樣式和大小
self.rgb_hex_label.setFixedSize(150, 50) # 設(shè)置固定大小 100x50
self.rgb_hex_label.setStyleSheet("font-size: 12px; padding: 5px; border-radius: 5px; background-color: white; border: 1px solid black;")
# 設(shè)置布局,使用 QHBoxLayout 和 QVBoxLayout 居中顯示標簽
layout = QVBoxLayout(self)
layout.setAlignment(Qt.AlignCenter) # 設(shè)置垂直布局居中
layout.addWidget(self.rgb_hex_label)
self.setLayout(layout)
# 定時器用于定時獲取顏色信息
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_color)
self.timer.start(100) # 每100毫秒更新一次
# 用來存儲背景顏色
self.bg_color = QColor(255, 255, 255) # 默認背景為白色
def update_color(self):
# 獲取鼠標位置
cursor_pos = QCursor.pos()
# 獲取屏幕截圖并獲取當前鼠標位置的顏色
screen = QApplication.primaryScreen()
pixmap = screen.grabWindow(0)
color = QColor(pixmap.toImage().pixel(cursor_pos))
# 獲取 RGB 和 16 進制顏色值
rgb = color.getRgb()
hex_color = color.name()
# 更新標簽顯示顏色信息
self.rgb_hex_label.setText(f"RGB: {rgb[0]}, {rgb[1]}, {rgb[2]}\nHex: {hex_color}")
# 改變窗口背景色
self.bg_color = color
# 刷新窗口
self.update()
def paintEvent(self, event):
# 繪制背景顏色
painter = QPainter(self)
painter.setBrush(QBrush(self.bg_color))
painter.setPen(Qt.NoPen)
painter.drawRect(self.rect()) # 填充整個窗口背景
painter.end()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ColorPickerApp()
window.show()
sys.exit(app.exec_())
到此這篇關(guān)于使用PyQt5編寫一個簡單的取色器的文章就介紹到這了,更多相關(guān)PyQt5取色器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)PDF轉(zhuǎn)換文本詳解
這篇文章主要介紹了詳解用Python把PDF轉(zhuǎn)換為文本方法總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-10-10
淺談Python3識別判斷圖片主要顏色并和顏色庫進行對比的方法
這篇文章主要介紹了淺談Python3識別判斷圖片主要顏色并和顏色庫進行對比的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python中棧、隊列與優(yōu)先級隊列的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Python中棧、隊列與優(yōu)先級隊列的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)根據(jù)字段將記錄分組操作示例
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)根據(jù)字段將記錄分組操作,結(jié)合實例形式分析了itertools.groupby()函數(shù)針對字典進行分組操作的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-03-03
Python WXPY實現(xiàn)微信監(jiān)控報警功能的代碼
本篇文章主要介紹了Python WXPY實現(xiàn)微信監(jiān)控報警功能的代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Python簡單實現(xiàn)查找一個字符串中最長不重復(fù)子串的方法
這篇文章主要介紹了Python簡單實現(xiàn)查找一個字符串中最長不重復(fù)子串的方法,涉及Python針對字符串的簡單遍歷、運算等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03

