[英]How to pass command to function from Tkinter Scale
I have a program with several sliders and need to run the same function for each of them but need to run based on which slider has been moved. 我有一个带有多个滑块的程序,需要为每个滑块运行相同的功能,但需要根据已移动的滑块运行。 How to I tell the function which slider was moved? 如何告诉功能哪个滑块被移动?
You do it like you do any other callback: use lambda
or functools.partial
to supply arguments. 您可以像执行其他任何回调一样进行操作:使用lambda
或functools.partial
提供参数。
For example: 例如:
import tkinter as tk
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
for scale in ("red", "green", "blue"):
widget = tk.Scale(self, from_=0, to=255, orient="horizontal",
command=lambda value, name=scale: self.report_change(name, value))
widget.pack()
def report_change(self, name, value):
print("%s changed to %s" % (name, value))
if __name__ == "__main__":
root=tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.