为了账号安全,请及时绑定邮箱和手机立即绑定

如何在scalewidget中插入时间戳

如何在scalewidget中插入时间戳

宝慕林4294392 2023-09-12 17:19:41
我怎样才能做到这一点 01:42我努力了:Scale(a,label='Current',from_='00:00',to='02:00',tickinterval='02:00',resolution='00:01',orient=HORIZONTAL,sliderlength=26,length=300)但没用
查看完整描述

1 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

不幸的是,您无法更改显示值的格式。您必须创建自己的标签,而不是使用小部件内置的标签。您可以通过将命令附加到小部件并让该命令更新标签来做到这一点。

由于您尝试表示时间,因此我建议将fromto选项设置为秒数,可以轻松地将其重新格式化为分钟和秒。

下面是一个自定义类的基本示例,它使用固定标签(即:它保持居中)并将值显示为分钟:秒。

https://img1.sycdn.imooc.com//65002d660001cc5301350106.jpg

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()


查看完整回答
反对 回复 2023-09-12
  • 1 回答
  • 0 关注
  • 46 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信