我一直在搞 Tkinter 并想出了这个:
![enter image description here](https://i.stack.imgur.com/lkX5z.png)
from tkinter import *
root = Tk()
def red_color_change():
color_label.configure(fg="red")
def blue_color_change():
color_label.configure(fg="blue")
red_button = Button(root, text="Red", fg="red", font="Arial, 20",
command=red_color_change)
red_button.grid(row=0, column=0)
blue_button = Button(root, text="Blue", fg="blue", font="Arial, 20",
command=blue_color_change)
blue_button.grid(row=0, column=1)
color_label = Label(root, text="Color", font="Arial, 20")
color_label.grid(row=1, columnspan=2)
root.mainloop()
我想知道如何简化red_color_change
and blue_color_change
成一个函数。其目标是通过一个函数更改彩色文本的颜色。
为什么不使用 lambda 表达式?
def color_change(color):
color_label.configure(fg=color)
red_button = Button(root, text="Red", fg="red", font="Arial, 20")
red_button.grid(row=0, column=0)
red_button.bind('<Button-1>', lambda e: color_change('red'))
blue_button = Button(root, text="Blue", fg="blue", font="Arial, 20")
blue_button.grid(row=0, column=1)
blue_button.bind('<Button-1>', lambda e: color_change('blue'))
这样就可以了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)