Rahul A Ranger
Rahul A Ranger

Reputation: 554

How to insert Timestamp in scalewidget

How can I make this 01:42

enter image description here

I have tried:

Scale(a,label='Current',from_='00:00',to='02:00',tickinterval='02:00',resolution='00:01',orient=HORIZONTAL,sliderlength=26,length=300)

but it didn't work

Upvotes: 0

Views: 179

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386230

Unfortunately, you can't change the format of the displayed value. You will have to create your own label instead of using the one built into the widget. You can do that by attaching a command to the widget, and having that command update the label.

Since you're trying to represent time, I recommend setting the from and to options to the number of seconds, which can easily be reformatted to minutes and seconds.

Here's a basic example of a custom class that uses a fixed label (ie: it stays centered) and displays the value as minutes:seconds.

screenshot

import tkinter as tk

class CustomScale(tk.Frame):
    def __init__(self, master, *args, **kwargs):
        from_ = kwargs.pop("from_", 0)
        to = kwargs.pop("to", 120)
        orient = kwargs.pop("orient", "vertical")
        super().__init__(*args, **kwargs)

        self.value_label = tk.Label(self, text="00:00")
        self.scale = tk.Scale(
            self, from_=from_, to=to, tickinterval=0,
            command=self.update_label, showvalue=False,
            orient=orient
        )
        self.value_label.pack(side="top", fill="x")
        self.scale.pack(side="bottom", fill="x")

    def update_label(self, value):
        value = int(value)
        minutes = value//60
        seconds = value%60

        self.value_label.configure(text=f"{minutes:02}:{seconds:02}")

root = tk.Tk()
scale = CustomScale(root, from_=0, to=120, orient="horizontal")

scale.pack(side="top", padx=20, pady=20)

root.mainloop()

Upvotes: 1

Related Questions