PyQt5 – QColorDialog
QColorDialog 它是一个颜色选择器小部件的对话框。颜色选择器是一个图形用户界面部件,通常在图形软件或在线中找到,用来选择颜色,有时也用来创建颜色方案。下面是QColorDialog的样子
它是PyQt5中的弹出式部件,它的基本用途是让用户选择颜色,这个部件在设计应用程序中非常有用,比如paint。
例子:
在这个例子中,我们将创建一个PyQt5应用程序,当执行时将打开颜色对话框,当用户选择颜色并关闭对话框时,将出现我们的主窗口,它有一个标签,其颜色将与对话框中选择的颜色相同。
下面是实现的过程
# 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())
输出 :