python实现颜色空间转换程序(Tkinter)

yipeiwu_com6年前Python基础

本文主要基于colorsys实现,样例是从hls转换到rgb,如果要换颜色空间很容易只需要修改一个函数,具体内容如下

用到了Scale和Canvas组件

运行效果图:

代码如下:

from Tkinter import * 
import colorsys 
#操作后的响应函数 
def update(* args): 
  'color' 
  r,g,b = colorsys.hls_to_rgb(h.get() / 255.0, l.get() / 255.0, s.get() / 255.0) 
  r,g,b = r * 255, g * 255, b * 255 
  rgb.configure(text = 'RGB:(%d, %d, %d)' % (r, g, b)) 
  c.configure(bg = '#%02X%02X%02X' %(r, g, b)) 
 
root = Tk() 
hue = Label(root, text = 'Hue') 
hue.grid(row = 0, column = 0) 
 
light = Label(root, text = 'Lightness') 
light.grid(row = 0, column = 1) 
 
sat = Label(root, text = 'Saturation') 
sat.grid(row = 0, column = 2) 
#初始化颜色为rgb的000,也就是纯黑色 
rgb = Label(root, text = 'RGB(0, 0, 0)') 
rgb.grid(row = 0, column = 3) 
 
 
h = Scale(root, from_ = 255, to = 0, command = update) 
h.grid(row = 1, column = 0) 
 
l = Scale(root, from_ = 255, to = 0, command = update) 
l.grid(row = 1, column = 1) 
 
s = Scale(root, from_ = 255, to = 0, command = update) 
s.grid(row = 1, column = 2) 
 
c = Canvas(root, width = 100, height = 100, bg = 'Black') 
c.grid(row = 1, column = 3) 
 
root.mainloop() 

以上就是本文的全部内容,希望对大家的学习Python程序设计有所帮助。

相关文章

Python实现Event回调机制的方法

Python实现Event回调机制的方法

0.背景 在游戏的UI中,往往会出现这样的情况: 在某个战斗副本中获得了某个道具A,那么当进入主界面的时候,你会看到你的背包UI上有个小红点(意思是有新道具),点击进入背包后,发现新增了...

python 监测内存和cpu的使用率实例

我就废话不多说了,直接上代码吧! import paramiko import pymysql import time linux = ['192.168.0.179'] def...

PyQt4 treewidget 选择改变颜色,并设置可编辑的方法

PyQt4 treewidget 选择改变颜色,并设置可编辑的方法

如下所示: # -*- coding: utf-8 -*- import sys from PySide.QtGui import * from PySide.QtCore impo...

简单了解python反射机制的一些知识

反射 反射机制就是在运行时,动态的确定对象的类型,并可以通过字符串调用对象属性、方法、导入模块,是一种基于字符串的事件驱动。 解释型语言:程序不需要编译,程序在运行时才翻译成机器语言,每...

利用selenium 3.7和python3添加cookie模拟登陆的实现

利用selenium 3.7和python3添加cookie模拟登陆的实现

前言 随着Python3的普及,Selenium3也跟上了行程。而Selenium3最大的变化是去掉了Selenium RC,另外就是Webdriver从各自浏览器中脱离,必须单独下载。...