QtGui.QColorDialog

he QtGui.QColorDialog provides a dialog widget for selecting colour values.

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example, we select a colour value
from the QtGui.QColorDialog and change the background
colour of a QtGui.QFrame widget. 

author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        col = QtGui.QColor(0, 0, 0) 

        self.btn = QtGui.QPushButton(‘Dialog‘, self)
        self.btn.move(20, 20)

        self.btn.clicked.connect(self.showDialog)

        self.frm = QtGui.QFrame(self)
        self.frm.setStyleSheet("QWidget { background-color: %s }"
            % col.name())
        self.frm.setGeometry(130, 22, 100, 100)            

        self.setGeometry(300, 300, 250, 180)
        self.setWindowTitle(‘Color dialog‘)
        self.show()

    def showDialog(self):

        col = QtGui.QColorDialog.getColor()

        if col.isValid():
            self.frm.setStyleSheet("QWidget { background-color: %s }"
                % col.name())

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == ‘__main__‘:
    main()

The application example shows a push button and a QtGui.QFrame. The widget background is set to black colour. Using the QtGui.QColorDialog, we can change its background.

col = QtGui.QColor(0, 0, 0)

This is an initial colour of the QtGui.QFrame background.

col = QtGui.QColorDialog.getColor()

This line will pop up the QtGui.QColorDialog.

if col.isValid():
    self.frm.setStyleSheet("QWidget { background-color: %s }"
        % col.name())

We check if the colour is valid. If we click on the Cancel button, no valid colour is returned. If the colour is valid, we change the background colour using style sheets.

Figure: Color dialog

时间: 2024-10-23 14:14:40

QtGui.QColorDialog的相关文章

PyQt4颜色对话框QColorDialog

QColorDialog提供了用于显示颜色的对话框. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import QtGui, QtCore class ColorDialog(QtGui.QWidget): def __init__(self, parent = None): QtGui.QWidget.__init__(self) color = QtGui.QColor(0, 0, 0) self.setGe

ZetCode PyQt4 tutorial Dialogs

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.QInputDialog dialog. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQ

pyqt 学习基础5 - 笔记软件的编辑器

笔记软件的编辑器 几个疑问 今天在试着为笔记软件写个编辑器,然后参考qt自带的textedit的示例.下面是一个撤销操作的一段代码,我就疑惑了,这跟用QPushButton应该差不多吧. self.actionUndo = QtGui.QAction( QtGui.QIcon.fromTheme('edit-undo', QtGui.QIcon(rsrcPath + '/editundo.png')), "&Undo", self, shortcut=QtGui.QKeySeq

Qt: 内建对话框(各种对话框都有了,且用到了qobject_cast解析sender的技术)

#include "BuiltinDialog.h" #include <QtGui/QTextEdit> #include <QtGui/QPushButton> #include <QtGui/QFileDialog> #include <QtGui/QFontDialog> #include <QtGui/QColorDialog> #include <QtGui/QPrintDialog> #include

关于Dialog----玲琅满目,任君挑选

下面来记录一下Qt中的形形色色的Dialg.... 直接给出PySide自带的example,因为我觉得我的改写也只是“依葫芦画瓢”...放心,Qt是开源的,真伟大.. #!/usr/bin/env python """PyQt4 port of the dialogs/standarddialogs example from Qt v4.x""" # This is only needed for Python v2 but is harmle

Qt标准对话框之QColorDialog

Qt中提供了一些标准的对话框,用于实现一些常用的预定义功能,比如本节中将要介绍的颜色对话框——QColorDialog. 在不同的系统平台下,颜色对话框的显示效果可能会有所不同,主要因系统主题风格而异,但是功能是相同的,显示效果如下: 查看帮助文档的话,我们可以看到这个类提供的方法还是很多的,但是我们一般情况下用到的也就一个静态的成员方法:getColor(),该函数的原型如下: 1 QColor QColorDialog::getColor(const QColor & initial = Q

【2017-06-17】QtGui基础控件:QSpinBox及QDoubleSpinBox

今天开始一个新的系列,主要是翻译并摘录QtGui中基础空间的常用方法,并做一些简单的实验程序: 我觉得这是一个炒冷饭的行为,但有时候冷饭不能不炒,不热不好吃,而且也很容易发霉. 其实到现在这种状态,对控件所提供的方法是否熟练已经不是问题,因为我们可以经常去看Manual,更为重要的是程序的框架和数据结构,如何把数据有效组织起来,如何组合或自定义控件来实现我们想要的功能,可能更是一种能力的体现,编程嘛,主要是来解决实际问题的. 希望能通过这个系列,我能对Qt的控件.实现方法有一个更深入的理解. Q

QtGui.QPixmap

A QtGui.QPixmap is one of the widgets used to work with images. It is optimized for showing images on screen. In our code example, we will use the QtGui.QPixmap to display an image on the window. #!/usr/bin/python # -*- coding: utf-8 -*- """

QtGui.QLineEdit

A QtGui.QLineEdit is a widget that allows to enter and edit a single line of plain text. There are undo and redo, cut and paste, and drag & drop functions available for the widget. #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode P