PyQt5 控件学习(一个一个学习之QColorDialog)

时间:2019-08-22
本文章向大家介绍PyQt5 控件学习(一个一个学习之QColorDialog),主要包括PyQt5 控件学习(一个一个学习之QColorDialog)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

QColorDialog的继承图:

QColorDialog的描述:

它整体的功能都是和QFontDialog 差不多的。

QColorDialog的继承:

它继承自 QDialog  

QColorDialog的功能作用:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setWindowTitle("选择一个好看的颜色")
        self.colorDialog.open()


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
构造函数

打开对话框,拿到用户选择的颜色:

此时要用到信号 :

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setWindowTitle("选择一个好看的颜色")

        self.colorDialog.colorSelected.connect(lambda color:print(color))

        self.colorDialog.open()


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

下面需求,用户选择的颜色,修改控件的背景颜色:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setWindowTitle("选择一个好看的颜色")
        def colorSelected_slot(color):
            #调色板
            palette = QPalette()
            palette.setColor(QPalette.Background,color)  # 颜色用于 背景
            #给窗口 设置调好的颜色
            self.setPalette(palette)

        self.colorDialog.colorSelected.connect(colorSelected_slot)
        self.colorDialog.open()


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
利用palette调色板给窗口设置背景颜色

另外open() 函数中还可以放一个槽函数,

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setWindowTitle("选择一个好看的颜色")
        def colorSelected_slot():
            #调色板
            palette = QPalette()

            # palette.setColor(QPalette.Background,self.colorDialog.currentColor())  # 颜色用于 背景
            palette.setColor(QPalette.Background,self.colorDialog.selectedColor())  # 颜色用于 背景
            #给窗口 设置调好的颜色
            self.setPalette(palette)

        # self.colorDialog.colorSelected.connect(colorSelected_slot)
        self.colorDialog.open(colorSelected_slot)  #注意它是不会将选择的color 传入槽函数的


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

补:

利用信号实时看到选择的颜色展示:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setWindowTitle("选择一个好看的颜色")
        def colorSelected_slot():
            #调色板
            palette = QPalette()

            palette.setColor(QPalette.Background,self.colorDialog.currentColor())  # 颜色用于 背景
            #给窗口 设置调好的颜色
            self.setPalette(palette)

        self.colorDialog.currentColorChanged.connect(colorSelected_slot)
        self.colorDialog.open()



if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
currentColorChanged 信号的使用

继续,除了open 之外,也可以使用exec ()  

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setWindowTitle("选择一个好看的颜色")
        def colorSelected_slot():
            #调色板
            palette = QPalette()

            palette.setColor(QPalette.Background,self.colorDialog.selectedColor())  # 颜色用于 背景
            #给窗口 设置调好的颜色
            self.setPalette(palette)

        if self.colorDialog.exec():
            colorSelected_slot()




if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code
from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setOptions(QColorDialog.NoButtons | QColorDialog.ShowAlphaChannel) #可以选择透明度
        self.colorDialog.setWindowTitle("选择一个好看的颜色")
        def colorSelected_slot():
            #调色板
            palette = QPalette()

            palette.setColor(QPalette.Background,self.colorDialog.selectedColor())  # 颜色用于 背景
            #给窗口 设置调好的颜色
            self.setPalette(palette)

        self.colorDialog.open()




if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

主要是自定义颜色和标准颜色,

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setOptions(QColorDialog.NoButtons | QColorDialog.ShowAlphaChannel) #可以选择透明度
        self.colorDialog.setWindowTitle("选择一个好看的颜色")
        self.colorDialog.open()

        print("静态方法 自定义颜色个数 customCount",QColorDialog.customCount())
        print("静态方法 设置自定义颜色 setCustomColor",QColorDialog.setCustomColor(0,QColor(10,200,20)))

        print("静态方法 设置标准颜色 setStandardColor",QColorDialog.setStandardColor(0,QColor(0,0,255)))

        


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

下面是最后一个静态方法:getColor() 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        self.btn = btn
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
            #getColor 会自动打开对话框
        color = self.colorDialog.getColor(QColor(255,0,0),self,"请选择颜色",QColorDialog.ShowAlphaChannel,)
        print(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_())
View Code

QColorDialog的信号:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        self.btn = btn
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setWindowTitle("请选择一个颜色")

        def colorDialog_selectedColor_slot(color):
            #创建调色板  改变按钮文本的颜色
            palette = QPalette()
            palette.setColor(QPalette.ButtonText,color)
            self.btn.setPalette(palette)
            
            
        #注意colorSelected  和selectedClor 的区别  后者不是信号

        self.colorDialog.colorSelected.connect(colorDialog_selectedColor_slot)



        self.colorDialog.open()





if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
colorSelected
from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QColorDialog的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        color = QColor(20,20,200)
        self.color = color
        colorDialog = QColorDialog(color,self)
        self.colorDialog = colorDialog

        self.test()
    def test(self):
        btn = QPushButton(self)
        self.btn = btn
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)

    def btn_clicked_slot(self):
        self.colorDialog.setWindowTitle("请选择一个颜色")

        def colorDialog_selectedColor_slot(color):
            #创建调色板  改变按钮文本的颜色
            palette = QPalette()
            palette.setColor(QPalette.ButtonText,color)
            self.btn.setPalette(palette)

        self.colorDialog.currentColorChanged.connect(colorDialog_selectedColor_slot)



        self.colorDialog.open()





if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
第二个信号

而且,当实时展示的时候,可以将按钮给去掉 !

总结:

以上就是颜色选择对话框,下面看文件选择对话框QFileDialog :https://www.cnblogs.com/zach0812/p/11392839.html

原文地址:https://www.cnblogs.com/zach0812/p/11392840.html