📜  PyQt5 - QColorDialog

📅  最后修改于: 2022-05-13 01:54:48.414000             🧑  作者: Mango

PyQt5 - QColorDialog

QColorDialog它是一个颜色选择器小部件的对话框。颜色选择器是一个图形用户界面小部件,通常在图形软件或在线中找到,用于选择颜色,有时用于创建配色方案。下面是 QColorDialog 的样子

它是 PyQt5 中的弹出式小部件,它的基本用途是允许用户选择颜色,这个小部件在设计绘画等应用程序时非常有用。

例子 :
在此我们将创建一个 PyQt5 应用程序,执行时将打开颜色对话框,当用户选择颜色并关闭对话框时,将出现我们的主窗口,该窗口有一个标签,其颜色将与对话框中选择的颜色相同.4

下面是实现

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 500, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
  
    # method for components
    def UiComponents(self):
  
        # opening color dialog
        color = QColorDialog.getColor()
  
        # creating label to display the color
        label = QLabel(self)
  
        # setting geometry to the label
        label.setGeometry(100, 100, 200, 60)
  
        # making label multi line
        label.setWordWrap(True)
  
        # setting stylesheet of the label
        label.setStyleSheet("QLabel"
                            "{"
                            "border : 5px solid black;"
                            "}")
  
        # setting text to the label
        label.setText(str(color))
  
        # setting graphic effect to the label
        graphic = QGraphicsColorizeEffect(self)
  
        # setting color to the graphic
        graphic.setColor(color)
  
        # setting graphic to the label
        label.setGraphicsEffect(graphic)
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :