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

最新文章

  1. C#开发微信门户及应用(39)--使用微信JSSDK实现签到的功能
  2. JVM内存管理------垃圾搜集器精解(让你在垃圾搜集器的世界里耍的游刃有余)
  3. screenshot of termbox cjk commit
  4. 遇到一位ITer,一位出租车司机,必看。
  5. HTML学习体会
  6. linux安全加固(2)
  7. angular 模板 小例子
  8. Gravitational Teleport 是一个先进的 SSH 服务器,基于 Golang SSH 构建,完全兼容 OpenSSH
  9. 推荐 15 个 Angular.js 应用扩展指令(参考应用)
  10. DeepID人脸识别算法之三代(转)
  11. Android 查看内存
  12. c++之 scanf 接收用户输入内容
  13. SSM增删改查
  14. asp+SqlServer2008开发【第四集:windows server 2008 r2的操作记录】
  15. mysql学习笔记--数据操作
  16. Block(一)基础-b
  17. 把AspDotNetCoreMvc程序运行在Docker上-part4:实现负载均衡
  18. 【六】注入框架RoboGuice使用:(Singletons And ContextSingletons)
  19. 吴恩达 Deep learning 第一周 深度学习概论
  20. java-length 、length()、size()的区别

热门文章

  1. hdu 4352 数位dp+nlogn的LIS
  2. bzoj 1776
  3. UESTC 2015dp专题 A 男神的礼物 区间dp
  4. Java程序生成linechart report的方法
  5. 【iOS开发-图层】图层的获取与一些属性
  6. sourceinsight使用技巧
  7. Eclipse maven构建springmvc项目
  8. 【资料】wod书籍
  9. MsChart<3> 饼状图
  10. OpenCV亚像素级的角点检测