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

yipeiwu_com4年前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将多个list合并为1个list的方法

Python将多个list合并为1个list的方法

1、可以使用"+"号完成操作 输出为: [1, 2, 3, 8, 'google', 'com'] 2、使用extend方法 输入相同 3、使用切片 输出相同 PS:len(l1)...

python编程线性回归代码示例

python编程线性回归代码示例

 用python进行线性回归分析非常方便,有现成的库可以使用比如:numpy.linalog.lstsq例子、scipy.stats.linregress例子、pandas.o...

python使用adbapi实现MySQL数据库的异步存储

python使用adbapi实现MySQL数据库的异步存储

之前一直在写有关scrapy爬虫的事情,今天我们看看使用scrapy如何把爬到的数据放在MySQL数据库中保存。 有关python操作MySQL数据库的内容,网上已经有很多内容可以参考了...

通过python下载FTP上的文件夹的实现代码

复制代码 代码如下:# -*- encoding: utf8 -*-import osimport sysimport ftplibclass FTPSync(object): ...

python 随机生成10位数密码的实现代码

随机生成10位数密码,字母和数字组合 import string >>> import random >>> pwd = "" >>&...