tkinter 的时间选择器 [英] Time picker for tkinter

查看:202
本文介绍了tkinter 的时间选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在 tkinter 应用程序中实现时间选择器的方法.

I am looking for a way to implement a time picker in a tkinter application.

我能够使用 spinbox 小部件和 @PRMoureu's 实现这一点(可能不是最好的方式)验证的绝妙答案.我现在拥有的是这个 -

I was able to implement this (probably not in the best way) using the spinbox widget and also using @PRMoureu's wonderful answer for validation. What I have right now is this -

import tkinter as tk

class App(tk.Frame):
    def __init__(self,parent):
        super().__init__(parent)
        self.reg=self.register(self.hour_valid)
        self.hourstr=tk.StringVar(self,'10')
        self.hour = tk.Spinbox(self,from_=0,to=23,wrap=True,validate='focusout',validatecommand=(self.reg,'%P'),invalidcommand=self.hour_invalid,textvariable=self.hourstr,width=2)
        self.reg2=self.register(self.min_valid)
        self.minstr=tk.StringVar(self,'30')
        self.min = tk.Spinbox(self,from_=0,to=59,wrap=True,validate='focusout',validatecommand=(self.reg2,'%P'),invalidcommand=self.min_invalid,textvariable=self.minstr,width=2)
        self.hour.grid()
        self.min.grid(row=0,column=1)
    def hour_invalid(self):
        self.hourstr.set('10')
    def hour_valid(self,input):
        if (input.isdigit() and int(input) in range(24) and len(input) in range(1,3)):
            valid = True
        else:
            valid = False
        if not valid:
            self.hour.after_idle(lambda: self.hour.config(validate='focusout'))
        return valid
    def min_invalid(self):
        self.minstr.set('30')
    def min_valid(self,input):
        if (input.isdigit() and int(input) in range(60) and len(input) in range(1,3)):
            valid = True
        else:
            valid = False
        if not valid:
            self.min.after_idle(lambda: self.min.config(validate='focusout'))
        return valid
root = tk.Tk()
App(root).pack()
root.mainloop()

这在 GUI 应用程序中似乎是一个非常普遍的要求,所以我认为必须有一种更标准的方法来实现这一点.如何以更简洁的方式实现用户选择的时间小部件?我问这个是因为我想要实现的小功能是在增加/减少分钟旋转框时,如果它循环结束,小时旋转框应该相应地增加/减少.我想通过设置回调函数来实现这一点,但我不知道确切触发了旋转框的哪个按钮(向上或向下).

This seems like a pretty common requirement in GUI applications so I think there must be a more standard way to achieve this. How can I implement a user picked time widget in a cleaner way? I am asking this because the tiny feature I want implemented is when incrementing/decrementing the minute-spinbox, if it loops over, the hour-spinbox should accordingly increase/decrease. I thought of achieving this by setting a callback function, but I would not come to know which button of the spinbox exactly was triggered (up or down).

推荐答案

您可以跟踪会议记录的变化并采取相应的行动.下面的示例显示了如何在分钟增加时自动增加小时 pass 59;你可以适应并弄清楚如何做减少部分.

You can trace the changes on your minutes and act accordingly. Below sample shows how to automatically increase hour when minutes increases pass 59; you can adapt and figure out how to do the decrease part.

import tkinter as tk

class App(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.hourstr=tk.StringVar(self,'10')
        self.hour = tk.Spinbox(self,from_=0,to=23,wrap=True,textvariable=self.hourstr,width=2,state="readonly")
        self.minstr=tk.StringVar(self,'30')
        self.minstr.trace("w",self.trace_var)
        self.last_value = ""
        self.min = tk.Spinbox(self,from_=0,to=59,wrap=True,textvariable=self.minstr,width=2,state="readonly")
        self.hour.grid()
        self.min.grid(row=0,column=1)

    def trace_var(self,*args):
        if self.last_value == "59" and self.minstr.get() == "0":
            self.hourstr.set(int(self.hourstr.get())+1 if self.hourstr.get() !="23" else 0)
        self.last_value = self.minstr.get()

root = tk.Tk()
App(root).pack()
root.mainloop()

这篇关于tkinter 的时间选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆