锋哥原创的PyQt6视频教程:
QColorDialog类表示颜色对话框,用户可以从中选择颜色。QColorDialog类最常用的方法是getColor()方法,用来获取在颜色对话框中选择的颜色信息。
UI生成测试代码:
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.pushButton = QtWidgets.QPushButton(parent=Form)
self.pushButton.setGeometry(QtCore.QRect(110, 130, 75, 23))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "请选择颜色..."))
Main测试代码:
"""
python加载ui文件
作者 : 小锋老师
官网 : www.python222.com
"""
import sys
from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox, QLineEdit, QInputDialog, QFontDialog, \
QColorDialog
from PyQt6 import uic, QtGui
def selectColor():
color, ok = QColorDialog.getColor()
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = uic.loadUi("./QColorDialog颜色对话框控件.ui")
pushButton: QPushButton = ui.pushButton
pushButton.clicked.connect(selectColor)
ui.show()
sys.exit(app.exec())