颜色对话框 QColorDialog 的案例
import sys
from PyQt5.QtCore import Qt, QRegExp
from PyQt5.QtGui import QIcon, QFont, QPalette, QPixmap, QIntValidator, QDoubleValidator, QRegExpValidator
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QToolTip, QHBoxLayout, QMainWindow, QLabel, QVBoxLayout, \
QDialog, QLineEdit, QGridLayout, QFormLayout, QTextEdit, QRadioButton, QCheckBox, QSlider, QSpinBox, QMessageBox, \
QInputDialog, QFontDialog, QColorDialog
'''
颜色对话框 QColorDialog 的案例
'''
class QDialogDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 400, 250)
# 设置窗口标题
self.setWindowTitle('字体对话框 FontDialog 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
#创建垂直布局
layout = QVBoxLayout()
#创建控件
self.colorButton = QPushButton('选择颜色')
self.colorButton.clicked.connect(self.getColor)
layout.addWidget(self.colorButton)
self.colorButton1 = QPushButton('选择背景颜色')
self.colorButton1.clicked.connect(self.getBGColor)
layout.addWidget(self.colorButton1)
self.colorLabel = QLabel('测试一下颜色显示')
layout.addWidget(self.colorLabel)
self.setLayout(layout)
def getColor(self):
color = QColorDialog.getColor()
p = QPalette()
p.setColor(QPalette.WindowText,color)
self.colorLabel.setPalette(p)
def getBGColor(self):
color = QColorDialog.getColor()
p = QPalette()
p.setColor(QPalette.Window,color)
self.colorLabel.setAutoFillBackground(True)
self.colorLabel.setPalette(p)
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = QDialogDemo()
w.show()
sys.exit(app.exec_())