简体   繁体   English

如何从Tkinter Scale传递命令以运行功能

[英]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. 您可以像执行其他任何回调一样进行操作:使用lambdafunctools.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.

相关问题 Python:如何在 Tkinter 中将函数作为变量命令传递 - Python: how to pass a function as a variable command in Tkinter 如何调用 function 并在 tkinter 比例小部件中传递变量? - How to call a function and pass variable in tkinter scale widget? 通过使用 tkinter 按钮命令,如何将 var 从 function 传递到主 function 中的本地 var - By using tkinter button command, how to pass var from function to local var in main function 如何从元组将方法传递给Tkinter按钮小部件命令选项 - How to pass methods to a Tkinter button widget command option from a tuple 我可以稍后在代码中为 tkinter 刻度设置命令 function 吗? - Can I set the command function for a tkinter scale later on in the code? Tkinter:如何将参数从入口传递到函数(以及退出时的故障) - Tkinter: How to pass arguments from entry to function (and glitch in exiting) 如何从 tkinter 中的回调 function 传递调用值 - how to pass call value from callback function in tkinter 如何从 tkinter Scale 小部件显示中删除比例值? - How to remove scale value from tkinter Scale widget display? 如何从第二个 window 在 tkinter 中将 function 作为按钮命令运行 - How to run a function as a button command in tkinter from a 2nd window 如何从Tkinter按钮获得命令功能? - How can the command function be obtained from a Tkinter button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM