python tkinter 日期时间选择器,附详细可运行源码

因为最近需要用的日期时间选择框,网上找了一圈发现大多都用不了,自己结合官方库写了一个,在这免费分享出来,供有需要的小伙伴使用。

先看效果图:

点击开始时间按钮弹出日期选择框,确定后输出到文本框中。

先安装官方库tkcalendar

下载网址:官方下载链接

官方文档有较详细的解释,下面附上本例中的代码。

如果喜欢的话点个赞吧~

#日期时间选择器
import tkinter as tk
from tkcalendar import Calendar
from tkinter import ttk
from tkinter import *
import datetime



def start_calendar():
    def print_sel():
        start_time_text.configure(state="normal")
        print(str(cal.selection_get()) + " " + str(hour.get()) + ":" + str(minute.get()))
        s_data = str(cal.selection_get()) + " " + str(hour.get()) + ":" + str(minute.get())
        start_time_text.delete(0, END)
        start_time_text.insert("0", s_data)
        start_time_text.configure(state="disabled")
        cal.see(datetime.date(year=2016, month=2, day=5))

    top = tk.Toplevel()
    top.geometry("300x250")

    today = datetime.date.today()

    mindate = datetime.date(year=2022, month=1, day=1)
    maxdate = today + datetime.timedelta(days=5)

    cal = Calendar(top, font="Arial 14", selectmode='day', locale='zh_CN', mindate=mindate, maxdate=maxdate,
                   background="red", foreground="blue", bordercolor="red", selectbackground="red",
                   selectforeground="red", disabledselectbackground=False)
    cal.place(x=0, y=0, width=300, height=200)
    value = 0
    values_h = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
                20, 21, 22, 23]
    values_m = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
                20, 21, 22, 23, 24, 25, 26, 27, 28,
                29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
                54,
                55, 56, 57, 58, 59]

    hour = ttk.Combobox(
        master=top,  # 父容器
        height=15,  # 高度,下拉显示的条目数量
        width=3,  # 宽度
        state="normal",  # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled
        cursor="arrow",  # 鼠标移动时样式 arrow, circle, cross, plus...
        font=("", 20),  # 字体
        values=values_h,  # 设置下拉框的选项
    )
    hour.place(x=0, y=200)
    ttk.Label(top, text="时").place(x=60, y=195, width=20, height=40)

    minute = ttk.Combobox(
        master=top,  # 父容器
        height=15,  # 高度,下拉显示的条目数量
        width=3,  # 宽度
        state="normal",  # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled
        cursor="arrow",  # 鼠标移动时样式 arrow, circle, cross, plus...
        font=("", 20),  # 字体
        values=values_m,  # 设置下拉框的选项
    )
    minute.place(x=80, y=200)
    ttk.Label(top, text="分").place(x=140, y=195, width=20, height=40)

    tk.Button(top, text="确定", command=print_sel).place(x=240, y=205)

root =Tk()
start_time = tk.Button(root, text="开始时间", command=start_calendar)
start_time.place(x=10,y=10)
start_time_text = tk.Entry(root, width=20)
start_time_text.place(x=100,y=10)
root.geometry("400x200")
root.mainloop()

 

  • 8
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

立秋6789

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值