【pyqt5】屏幕取色器

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QColor, QGuiApplication, QCursor
from PyQt5.QtCore import QTimer
from getColor import Ui_Form


class ColorPickerApp(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.showColorValue()

    def showColorValue(self):
        # 获取鼠标的X和Y坐标
        x = QCursor.pos().x()
        y = QCursor.pos().y()

        # 将鼠标的X和Y坐标显示在文本框中
        self.ui.txtXY.setText("X:{} Y:{}".format(x, y))
        strDecimalValue, strHex = "", ""

        # 获取当前屏幕
        screen = QGuiApplication.primaryScreen()
        # 获取当前屏幕的像素
        pixmap = screen.grabWindow(QApplication.desktop().winId(), x, y, 2, 2)

        # 如果像素不为空,则获取像素的颜色
        if not pixmap.isNull():
            image = pixmap.toImage()
            if not image.isNull() and image.valid(0, 0):
                color = QColor(image.pixel(0, 0))
                red = color.red()
                green = color.green()
                blue = color.blue()

                # 将颜色转换为16进制
                strRed = "{:02X}".format(red & 0xFF)
                strGreen = "{:02X}".format(green & 0xFF)
                strBlue = "{:02X}".format(blue & 0xFF)

                # 将颜色转换为十进制
                strDecimalValue = "{}, {}, {}".format(red, green, blue)
                strHex = "#{:02X}{:02X}{:02X}".format(
                    int(strRed, 16), int(strGreen, 16), int(strBlue, 16))

        # 设置背景颜色
        strStyle = "background-color: rgb({});".format(strDecimalValue)
        self.ui.labColor.setStyleSheet(strStyle)
        # 将十进制颜色值显示在文本框中
        self.ui.txtRgb.setText(strDecimalValue)
        # 将十六进制颜色值显示在文本框中
        self.ui.txtWeb.setText(strHex)

        # 每隔100毫秒调用一次showColorValue函数
        QTimer.singleShot(100, self.showColorValue)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    color_picker = ColorPickerApp()
    color_picker.show()
    sys.exit(app.exec_())

getColor.py

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(362, 168)
        self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.labColor = QtWidgets.QLabel(Form)
        self.labColor.setMinimumSize(QtCore.QSize(140, 140))
        self.labColor.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
        self.labColor.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.labColor.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.labColor.setText("")
        self.labColor.setObjectName("labColor")
        self.horizontalLayout.addWidget(self.labColor)
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.label = QtWidgets.QLabel(Form)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.txtWeb = QtWidgets.QLineEdit(Form)
        self.txtWeb.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 170, 0);")
        self.txtWeb.setObjectName("txtWeb")
        self.gridLayout.addWidget(self.txtWeb, 0, 1, 1, 1)
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
        self.txtRgb = QtWidgets.QLineEdit(Form)
        self.txtRgb.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 170, 0);")
        self.txtRgb.setObjectName("txtRgb")
        self.gridLayout.addWidget(self.txtRgb, 1, 1, 1, 1)
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
        self.txtXY = QtWidgets.QLineEdit(Form)
        self.txtXY.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 170, 0);")
        self.txtXY.setText("")
        self.txtXY.setObjectName("txtXY")
        self.gridLayout.addWidget(self.txtXY, 2, 1, 1, 1)
        self.horizontalLayout.addLayout(self.gridLayout)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.labColor.setToolTip(_translate("Form", "双击复制颜色值"))
        self.label.setText(_translate("Form", "Web值:"))
        self.label_2.setText(_translate("Form", "Rgb值:"))
        self.label_3.setText(_translate("Form", "坐标值:"))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值