1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python tkinter 日期时间选择器 附详细可运行源码

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

时间:2021-07-19 01:31:39

相关推荐

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

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

先看效果图:

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

先安装官方库tkcalendar

下载网址:官方下载链接

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

如果喜欢的话点个赞吧~

#日期时间选择器import tkinter as tkfrom tkcalendar import Calendarfrom tkinter import ttkfrom tkinter import *import datetimedef 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=, month=2, day=5))top = tk.Toplevel()top.geometry("300x250")today = datetime.date.today()mindate = datetime.date(year=, 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 = 0values_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 = bobox(master=top, # 父容器height=15, # 高度,下拉显示的条目数量width=3, # 宽度state="normal", # 设置状态 normal(可选可输入)、readonly(只可选)、 disabledcursor="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 = bobox(master=top, # 父容器height=15, # 高度,下拉显示的条目数量width=3, # 宽度state="normal", # 设置状态 normal(可选可输入)、readonly(只可选)、 disabledcursor="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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。