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

QtGui.QColorDialog的更多相关文章

  1. PyQt4颜色对话框QColorDialog

    QColorDialog提供了用于显示颜色的对话框. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import Qt ...

  2. PyQt4入门学习笔记(五)

    PyQt4里的对话框 对话框是大多数GUI应用中不可分割的一部分.一个对话框是两者或多者的会话.在GUI内,对话框是应用向人说话的方式.一个对话框可以用来输入数据,修改数据,改变应用设置等等. QtG ...

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

    #include "BuiltinDialog.h" #include <QtGui/QTextEdit> #include <QtGui/QPushButton ...

  4. Python -- Gui编程 -- Qt库的使用 -- 菜单与对话框

    1.菜单 import sys from PyQt4 import QtCore, QtGui class MyWindow(QtGui.QMainWindow): def __init__(self ...

  5. QDialog:输入对话框、颜色对话框、字体对话框、文件对话框

    # _*_ coding:utf-8 _*_ import sys from PyQt4 import QtCore,QtGui class Example(QtGui.QWidget): def _ ...

  6. ZetCode PyQt4 tutorial Dialogs

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  7. PyQt4 颜色选择,字体选择代码

    # -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...

  8. 读Pyqt4教程,带你入门Pyqt4 _005

    对话框窗体或对话框是现代GUI应用不可或缺的一部分.dialog定义为两个或多个人之间的交谈.在计算机程序中dialog是一个窗体,用来和程序“交谈”.对话框用来输入数据.修改数据.改变程序设置等等. ...

  9. 六、pyqt5对话框——QInputDialog、QColorDialog、QFontDialog、QMessageBox、QFileDialog

    目录:  一.对话框综合示例 二.QDialog 三.QInputDialog 四.QMessageDialog 五.QFileDialog pyqt5的对话框有多种类型,比如输入对话框(QInput ...

随机推荐

  1. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

  2. MSSQL SELECT(刚刚)新插入到表中的那条记录

    假设对表 TXxxxxxxx 表新插入一条记录,然后要 SELECT 出刚刚插入的这条记录.可使用 SCOPE_IDENEITY(); 处理.具体代码参考如下: INSERT INTO TXxxxxx ...

  3. USACO 2017 FEB Platinum nocross DP

    题目大意 上下有两个长度为n.位置对应的序列A.B,其中数的范围均为1~n.若abs(A[i]-B[j]) <= 4,则A[i]与B[j]间可以连一条边.现要求在边与边不相交的情况下的最大的连边 ...

  4. svm算法介绍

    在一个理想的分类当中,我们想要用一个超平面来将正类样本和负类样本划分开来.这个超平面的方程为 $\mathbf{w}^T\mathbf{x}+b=0$ 我们希望这个超平面能够使得划分更加的鲁棒,在图形 ...

  5. EM(Expectation Maximization)算法

    EM(Expectation Maximization)算法  参考资料: [1]. 从最大似然到EM算法浅解 [2]. 简单的EM算法例子 [3]. EM算法)The EM Algorithm(详尽 ...

  6. CentOS 6.9/7通过yum安装指定版本的Tomcat

    说明:通过yum好处其实很多,环境变量不用配置,配置文件放在大家都熟悉的地方,通过rpm -ql xxx可以知道全部文件的地方等等. 一.安装Tomcat(8+) // 下载脚本 # git clon ...

  7. iOS开发之int,NSInteger,NSUInteger,NSNumber的使用

    1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...

  8. eclipse的项目和配置文件 .project .cproject .classpath .metadata

    eclipse CDT建立project后在project name对应的目录下面会生成.project和.cproject两个隐藏文件. eclipse  java建立project后在projec ...

  9. eclipse中配置struts2出现There is no Action mapped for namespace [/] and action name [Login] associated wi

    下午在eclipse中配置struts2时报: There is no Action mapped for namespace [/] and action name [Login] associat ...

  10. 红帽子企业版 7.3 INSTALL SYSTEMTAP

    [root@workstation2017 ~]# uname -r 1.安装程序 yum install systemtap systemtap-runtime // 这个会被安装 systemta ...