凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

19. PyQt6 QColorDialog

2023-10-17 python 348
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QFrame, QColorDialog
from PyQt6.QtGui import QGuiApplication, QColor


class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.btn = None
        self.frame = None
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt6 QColorDialog")

        color = QColor(0, 0, 0)
        self.btn = QPushButton("选择颜色", self)
        self.btn.move(20, 20)
        self.btn.clicked.connect(self.showDialog)

        self.frame = QFrame(self)
        self.frame.setStyleSheet("QWidget {background-color:%s}" % color.name())
        self.frame.setGeometry(130, 22, 450, 350)

        self.setGeometry(100, 100, 400, 300)
        self.center()

    def showDialog(self):
        color = QColorDialog.getColor()
        if color.isValid():
            self.frame.setStyleSheet("QWidget {background-color:%s}" % color.name())

    def center(self):
        qr = self.frameGeometry()
        cp = QGuiApplication.primaryScreen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    cls_app = App()
    cls_app.show()
    sys.exit(app.exec())

image.png

文章评论

0条评论