本文主要基于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中的tkinter模块作图的方法python3.5使用tkinter制作记事本基于python的Tkinter实现一个简易计算器python开发之tkinter实现图形随鼠标移动的方法Python基于Tkinter实现的记事本实例Python基于Tkinter的HelloWorld入门实例python基于Tkinter库实现简单文本编辑器实例Python编程使用tkinter模块实现计算器软件完整代码示例