2016-01-08 28 views
0

我有一個帶有幾個滑塊的程序,需要爲它們中的每一個運行相同的函數,但需要根據哪個滑塊移動來運行。如何告訴函數哪個滑塊被移動了?如何將命令傳遞到Tkinter Scale的函數

+0

如果使用綁定'()'代替'命令=','的參數event'應該有指示哪個插件引發事件的屬性。 – Kevin

回答

1

你這樣做就像你做任何其他回調:使用lambdafunctools.partial來提供參數。

例如:

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() 
相關問題