Dynamically reformat tkinter scale value python 2.7
Question: I thought what I wanted to do would be simple enough, but evidently not.

What I want to do is use a tkinter scale to control the range and value that a
user can input.

In my case I want to input a time value in seconds and display it in
_minutes:seconds_ format so that anybody can understand that, say 330 seconds
== 5:30.

Because this is not a standard format for the scale widget what I want to do
is make the time in mm:ss format appear beside the scale widget.

In my example code I can see the scale value changing, but so far I can not
get the mm:ss display to change as the scale is moved (I've got to click a
button to get it to update). As I want the end result to be as idiot proof as
possible I need the mm:ss display to change dynamically with the slider.

At this stage I appear to have exhausted all the online examples I can find,
and none of them appear to do what I want (extra button press required for
conversion).

I'm pretty sure I'll feel stupid when I find out how to do this, but right now
my head hurts from trying to figure this out.

Does anyone have an example of this behaviour that they can share?


Answer: The scale widget has a `command` attribute which you can use to call a
function whenever the value changes. This function can then reformat the value
to whatever you want.

    
    
    import Tkinter as tk
    
    class Example(tk.Frame):
        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
    
            self.scale = tk.Scale(self, orient="horizontal", 
                                  from_=0, to=600, 
                                  showvalue=False,
                                  command=self._on_scale)
            self.scale_label = tk.Label(self, text="")
            self.scale.pack(side="top", fill="x")
            self.scale_label.pack(side="top")
    
        def _on_scale(self, value):
            value = int(value)
            minutes = value/60
            seconds = value%60
            self.scale_label.configure(text="%2.2d:%2.2d" % (minutes, seconds))
    
    if __name__ == "__main__":
        root = tk.Tk()
        Example(root).pack(fill="both", expand=True);
        root.mainloop()
    

