Dynamically change tkinter python 2.7 scaling value

I thought that what I wanted to do would be simple enough, but obviously not.

I want to use the tkinter scale to control the range and value that the user can enter.

In my case, I want to enter the time value in seconds and display it in minutes: the format of seconds so that someone can understand this, say 330 seconds == 5:30.

Since this is not the standard format of the scale widget that I want to do is specify the time in mm: ss format next to the scale widget.

In my code example, I see a change in the scale value, but so far I can’t change the display of mm: ss when zooming (I have to click a button to update it). Since I want the end result to be an idiot as much as possible, I need the mm: ss display to dynamically change with the slider.

At this point, I seem to have exhausted all the online examples that I can find, and none of them do what I want (an extra click of the button is required for the conversion).

I am sure that I will feel stupid when I learn how to do it, but now my head hurts, trying to understand it.

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

0
source share
1 answer

The scale widget has a command attribute that you can use to call a function whenever the value changes. This function can then reformat the value as 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() 
+2
source

Source: https://habr.com/ru/post/1270171/


All Articles