使用PyQt5編寫(xiě)一個(gè)簡(jiǎn)單的取色器
PyQt5搭建的一個(gè)取色器,一共寫(xiě)了兩款應(yīng)用,一款使用快捷鍵捕獲鼠標(biāo)附近圖像的RGB和16進(jìn)制顏色編碼,一款跟隨鼠標(biāo)刷新圖像的RGB和16進(jìn)制顏色編碼。桌面應(yīng)用程序的背景色切換也可以參考此程序。
源程序的git地址: gitee.com/mtoooo/color_picker
打包的exe下載鏈接: gitee.com/mtoooo/color_picker
取色器1
源代碼參考main.py
,也可以點(diǎn)擊頂部exe鏈接下載取色器.exe
文件直接使用,取色快捷鍵Shift+A
,應(yīng)用程序會(huì)顯示RGB和16進(jìn)制顏色編碼。
源程序初始化
pip install PyQt5==5.15.10
程序啟動(dòng)
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)建標(biāo)簽,用于顯示 RGB 和 16 進(jìn)制顏色 self.rgb_hex_label = QLabel("RGB: None\nHex: None", self) # 設(shè)置標(biāo)簽樣式和大小 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 居中顯示標(biāo)簽 layout = QVBoxLayout(self) layout.setAlignment(Qt.AlignCenter) # 設(shè)置垂直布局居中 layout.addWidget(self.rgb_hex_label) self.setLayout(layout) # 定時(shí)器用于定時(shí)獲取顏色信息 self.timer = QTimer(self) self.timer.timeout.connect(self.update_color) self.timer.start(100) # 每100毫秒更新一次 # 用來(lái)存儲(chǔ)背景顏色 self.bg_color = QColor(255, 255, 255) # 默認(rèn)背景為白色 def update_color(self): # 獲取鼠標(biāo)位置 cursor_pos = QCursor.pos() # 獲取屏幕截圖并獲取當(dāng)前鼠標(biāo)位置的顏色 screen = QApplication.primaryScreen() pixmap = screen.grabWindow(0) color = QColor(pixmap.toImage().pixel(cursor_pos)) # 獲取 RGB 和 16 進(jìn)制顏色值 rgb = color.getRgb() hex_color = color.name() # 更新標(biāo)簽顯示顏色信息 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()) # 填充整個(gè)窗口背景 painter.end() if __name__ == "__main__": app = QApplication(sys.argv) window = ColorPickerApp() window.show() sys.exit(app.exec_())
取色器2
源代碼參考main2.py
, 也可以下載取色器2.exe
文件直接使用,無(wú)需快捷鍵運(yùn)行即可使用,應(yīng)用程序會(huì)顯示RGB和16進(jìn)制顏色編碼。
- 源程序初始化
pip install PyQt5==5.15.10
程序啟動(dòng)
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)建標(biāo)簽,用于顯示 RGB 和 16 進(jìn)制顏色 self.rgb_hex_label = QLabel("RGB: None\nHex: None", self) # 設(shè)置標(biāo)簽樣式和大小 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 居中顯示標(biāo)簽 layout = QVBoxLayout(self) layout.setAlignment(Qt.AlignCenter) # 設(shè)置垂直布局居中 layout.addWidget(self.rgb_hex_label) self.setLayout(layout) # 定時(shí)器用于定時(shí)獲取顏色信息 self.timer = QTimer(self) self.timer.timeout.connect(self.update_color) self.timer.start(100) # 每100毫秒更新一次 # 用來(lái)存儲(chǔ)背景顏色 self.bg_color = QColor(255, 255, 255) # 默認(rèn)背景為白色 def update_color(self): # 獲取鼠標(biāo)位置 cursor_pos = QCursor.pos() # 獲取屏幕截圖并獲取當(dāng)前鼠標(biāo)位置的顏色 screen = QApplication.primaryScreen() pixmap = screen.grabWindow(0) color = QColor(pixmap.toImage().pixel(cursor_pos)) # 獲取 RGB 和 16 進(jìn)制顏色值 rgb = color.getRgb() hex_color = color.name() # 更新標(biāo)簽顯示顏色信息 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()) # 填充整個(gè)窗口背景 painter.end() if __name__ == "__main__": app = QApplication(sys.argv) window = ColorPickerApp() window.show() sys.exit(app.exec_())
到此這篇關(guān)于使用PyQt5編寫(xiě)一個(gè)簡(jiǎn)單的取色器的文章就介紹到這了,更多相關(guān)PyQt5取色器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3中正則模塊re.compile、re.match及re.search函數(shù)用法詳解
這篇文章主要介紹了Python3中正則模塊re.compile、re.match及re.search函數(shù)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了re模塊 中re.compile、re.match及re.search函數(shù)的功能、參數(shù)、具體使用技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06pytorch中的reshape()、view()、nn.flatten()和flatten()使用
這篇文章主要介紹了pytorch中的reshape()、view()、nn.flatten()和flatten()使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Python 3.8正式發(fā)布,來(lái)嘗鮮這些新特性吧
今天 Python3.8 發(fā)布啦,它是 Python2 終結(jié)前最后一個(gè)大版本,我們一起看看這個(gè)版本都添加了那些新功能和特性2019-10-10python利用urllib實(shí)現(xiàn)爬取京東網(wǎng)站商品圖片的爬蟲(chóng)實(shí)例
下面小編就為大家?guī)?lái)一篇python利用urllib實(shí)現(xiàn)爬取京東網(wǎng)站商品圖片的爬蟲(chóng)實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法
這篇文章主要介紹了python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法,涉及使用PIL模塊操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-05-05Python利用capstone實(shí)現(xiàn)反匯編
Capstone是一個(gè)輕量級(jí)的多平臺(tái)、多架構(gòu)的反匯編框架,該模塊支持目前所有通用操作系統(tǒng),反匯編架構(gòu)幾乎全部支持。本文就將利用他實(shí)現(xiàn)反匯編,感興趣的可以了解下2022-04-04Pandas缺失值填充 df.fillna()的實(shí)現(xiàn)
本文主要介紹了Pandas缺失值填充 df.fillna()的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07使用Python程序抓取新浪在國(guó)內(nèi)的所有IP的教程
這篇文章主要介紹了使用Python程序抓取新浪在國(guó)內(nèi)的所有IP的教程,作為Python網(wǎng)絡(luò)編程中獲取IP的一個(gè)小實(shí)踐,需要的朋友可以參考下2015-05-05