PyQt5+python取色器

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class MainWnd(QWidget):
    def __init__(self, parent=None):
        super(MainWnd, self).__init__(parent)
        self.initUI()
        timer = QTimer(self)
        timer.timeout.connect(self.slotPickColor)
        timer.start(20)

    def initUI(self):
        mainLayout = QGridLayout(self)
        self.labColor = QLabel()
        self.labColor.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
        self.labColor.setMinimumSize(80, 80)
        mainLayout.addWidget(self.labColor, 0, 0, 3, 1)
        
        labs = list(map(lambda text: QLabel(text), ['RGB值', 'css值', '坐标值:']))
        list(map(lambda lab: lab.setAlignment(Qt.AlignRight | Qt.AlignVCenter), labs))
        list(map(lambda lab, row: mainLayout.addWidget(lab, row, 1, 1, 1), labs, range(3)))
       
        self.txtRGB, self.txtCSS, self.txtXY = txts = list(map(lambda i: QLineEdit(), range(3)))
        list(map(lambda txt: txt.setStyleSheet('background-color:rgb(0,0,0);color:rgb(255,170,1)'), txts))
        list(map(lambda txt, row: mainLayout.addWidget(txt, row, 2, 1, 1), txts, range(3)))
        
        self.resize(285, 120)
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())
        self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint | Qt.WindowStaysOnTopHint)

        self.setWindowTitle('吸色器')

    def slotPickColor(self):
        x = QCursor.pos().x()
        y = QCursor.pos().y()
        
        self.txtXY.setText('x:%d y:%d'%(x, y))
        pixmap = QApplication.primaryScreen().grabWindow(QApplication.desktop().winId(), x, y, 1, 1)
        
        image = pixmap.toImage()
        color = QColor(image.pixel(0, 0))
        r, g, b = color.red(), color.green(), color.blue()
        
        strRGB = '%d,%d,%d'%(r, g, b)
        self.txtRGB.setText(strRGB)
        self.labColor.setStyleSheet('background-color:rgb(%s)'%(strRGB))

        hexs = list(map(lambda x: str(hex(x)).replace('0x', '').upper(), [r, g, b]))
        strCSS = '#{:0>2s}{:0>2s}{:0>2s}'.format(*hexs)
        self.txtCSS.setText(strCSS)

def main():
    app = QApplication(sys.argv)
    w = MainWnd()
    w.show()
    sys.exit(app.exec_())

main()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值