Today's talk is QColorDialog dialog box.
I. Description
QColorDialog dialog box controls are used to provide users with choice of colors, and the last chapter of QFontDialog controls, QDialog is inherited from the base class. Using substantially the same methods and QFontDialog.
II. Function
1. Constructor
Can directly use the statement, you can pass it a QColor object as the default color
color = QColor(30,40,50)
color_dialog =QColorDialog(color,self)
Here creates an RGB color value of the object 30, 40, then there will be a pop-up dialog box to see the default color
2. Open Dialog
Open dialog There are three common methods:
QColorDialog.show() QColorDialog.open() QColorDialog.exec()
If a show () method can then transmit this signal colorSelected
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.UI_test() self.resize(800,500) def UI_test(self): btn = QPushButton('color',self) btn.move(50,50) btn.clicked.connect(self.fun) self.btn = btn pass def fun(self): color = QColor(30,40,50) color_dialog =QColorDialog(color,self) color_dialog.colorSelected.connect(self.change_color) color_dialog.show() def change_color(self,color): palette = QPalette() palette.setColor(QPalette.Background,color) self.setPalette(palette) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
While open () is a direct function of the transfer grooves, and the groove is not a function of the transmission parameters. It can only be obtained manually.
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.UI_test() self.resize(800,500) def UI_test(self): btn = QPushButton('color',self) btn.move(50,50) btn.clicked.connect(self.fun) self.btn = btn pass def fun(self): color = QColor(30,40,50) color_dialog =QColorDialog(color,self) color_dialog.colorSelected.connect(self.change_color) # color_dialog.show() color_dialog.open(self.change_color) self.col_dia = color_dialog def change_color(self): palette = QPalette() palette.setColor(QPalette.Background,self.col_dia.selectedColor()) #直接获取颜色值 self.setPalette(palette) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
exec is to have a return value, just click on the OK button returns a value of 1. This value can be used to execute the corresponding operation.
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.UI_test() self.resize(800,500) def UI_test(self): btn = QPushButton('color',self) btn.move(50,50) btn.clicked.connect(self.fun) self.btn = btn pass def fun(self): color = QColor(30,40,50) color_dialog =QColorDialog(color,self) color_dialog.colorSelected.connect(self.change_color) # color_dialog.show() self.col_dia = color_dialog if self.col_dia.exec(): #利用exec的返回值 self.change_color() def change_color(self): palette = QPalette() palette.setColor(QPalette.Background,self.col_dia.selectedColor()) #直接获取颜色值 self.setPalette(palette) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
3. Control Options
QColorDialog.setOption (Self, Option: ' QColorDialog.ColorDialogOption ' , ON: BOOL = ...) QColorDialog.setOptions (Self, Options: typing.Union [ ' QColorDialog.ColorDialogOptions ' , ' QColorDialog.ColorDialogOption ' ]) QColorDialog.testOption (Self, Option: ' QColorDialog.ColorDialogOption ' ) # -> BOOL # type: 'QColorDialog.ColorDialogOption' ShowAlphaChannel # allows selection of components Alpha (transparency) NoButtons # cancellation confirmation, a cancel button DontUseNativeDialog # Do not use the native color dialog box and choose Qt standard color dialog
NoButtons options which often change with the current color to change the color of the real-time action
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.UI_test() self.resize(800,500) def UI_test(self): btn = QPushButton('color',self) btn.move(50,50) btn.clicked.connect(self.fun) self.btn = btn pass def fun(self): color = QColor(30,40,50) color_dialog =QColorDialog(color,self) color_dialog.colorSelected.connect(self.change_color) color_dialog.setOptions(QColorDialog.NoButtons | QColorDialog.ShowAlphaChannel) #取消按钮,开启透明度 color_dialog.currentColorChanged.connect(self.change_color) #当前颜色发生变化 self.col_dia = color_dialog color_dialog.show() def change_color(self,col): palette = QPalette() palette.setColor(QPalette.Background,col) self.setPalette(palette) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
4. Static method
First, there is a concept: standard colors and custom colors. The standard color is the color dialog box provides (Basic Colors), and custom color is the color of the following user-defined (Custom Colors).
Custom colors can use a common set of 18. On to the order from the upper left is below 0,0 1, 2 is right.
QColorDialog.setCustomColor(index: int,color: typing.Union[QtGui.QColor, QtCore.Qt.GlobalColor, QtGui.QGradient])
QColorDialog.setStandardColor(index: int, color: typing.Union[QtGui.QColor, QtCore.Qt.GlobalColor, QtGui.QGradient])
This two-day designated color code of the color of the grid, we should note that this must be instantiated in the control as previously defined.
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.UI_test() def UI_test(self): QColorDialog.setCustomColor(1,QColor(50,60,70)) cd = QColorDialog(self) cd.show() pass if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())