PyQt5 – QColorDialog
QColorDialog It is a dialog box of a color picker widget. A color picker is a graphical user interface widget, usually found within graphics software or online, used to select colors and sometimes to create color schemes. Below is how the QColorDialog looks like
It is popup type widget in PyQt5 it basic use is to allow user to choose the color this widget is very useful in designing application like paint.
Example :
In this we will create a PyQt5 application which when executed will open the color dialog and when user choose the color and close the dialog there will be our main window which has a label and its color will be same as of the color selected in the dialog.4
Below is the implementation
# 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 ()) |
Output :
