当前位置: 首页 > 工具软件 > scale9grid > 使用案例 >

python中scale函数_Python Tkinter Scale lambda函数

顾炎彬
2023-12-01

Scale提供x。当你给Scale小部件一个函数时,它用当前值调用该函数。在

我将以一种理智的方式重写您的代码,这样您可能会更好地遵循它:from tkinter import *

color = [0,0,0] # standard red, green, blue (RGB) color triplet

def convert_color(color):

'''converts a list of 3 rgb colors to an html color code

eg convert_color(255, 0, 0) -> "#FF0000

'''

return '#{:02X}{:02X}{:02X}'.format(color[0],color[1],color[2])

def show_color(value, index):

'''

update one of the color triplet values

index refers to the color. Index 0 is red, 1 is green, and 2 is blue

value is the new value'''

color[int(index)] = int(value) # update the global color triplet

hex_code = convert_color(color) # convert it to hex code

color_label.configure(bg=hex_code) #update the color of the label

color_text.configure(text='RGB: {!r}\nHex code: {}'.format(color, hex_code)) # update the text

def update_red(value):

show_color(value, 0)

def update_green(value):

show_color(value, 1)

def update_blue(value):

show_color(value, 2)

root = Tk()

red_slider = Scale(orient='horizontal',from_=0,to=255,command=update_red)

red_slider.grid(row=0,column=0)

green_slider = Scale(orient='horizontal',from_=0,to=255,command=update_green)

green_slider.grid(row=1,column=0)

blue_slider = Scale(orient='horizontal',from_=0,to=255,command=update_blue)

blue_slider.grid(row=2,column=0)

color_text = Label(justify=LEFT)

color_text.grid(row=3,column=0, sticky=W)

color_label = Label(bg=convert_color(color),width=20)

color_label.grid(row=4,column=0)

mainloop()

 类似资料: