Python tkinter 下拉日历控件代码


Posted in Python onMarch 04, 2020

tkinter 下拉日历控件

网上找的不完善的Tk日历进行修改的,可以快捷的找到并返回日期。

效果如下图,上面的是控件,下面的是调用demo窗口

Python tkinter 下拉日历控件代码

如下所示:

# -*- coding: utf-8 -*- 
import calendar
import tkinter as tk
import tkinter.font as tkFont
from tkinter import ttk

datetime = calendar.datetime.datetime
timedelta = calendar.datetime.timedelta

class Calendar:

  def __init__(s, point = None, position = None):
    # point  提供一个基点,来确定窗口位置
    # position 窗口在点的位置 'ur'-右上, 'ul'-左上, 'll'-左下, 'lr'-右下
    #s.master = tk.Tk()
    s.master = tk.Toplevel()
    s.master.withdraw()
    fwday = calendar.SUNDAY

    year = datetime.now().year
    month = datetime.now().month
    locale = None
    sel_bg = '#ecffc4'
    sel_fg = '#05640e'

    s._date = datetime(year, month, 1)
    s._selection = None # 设置为未选中日期

    s.G_Frame = ttk.Frame(s.master)

    s._cal = s.__get_calendar(locale, fwday)

    s.__setup_styles()    # 创建自定义样式
    s.__place_widgets()   # pack/grid 小部件
    s.__config_calendar()  # 调整日历列和安装标记
    # 配置画布和正确的绑定,以选择日期。
    s.__setup_selection(sel_bg, sel_fg)

    # 存储项ID,用于稍后插入。
    s._items = [s._calendar.insert('', 'end', values='') for _ in range(6)]

    # 在当前空日历中插入日期
    s._update()

    s.G_Frame.pack(expand = 1, fill = 'both')
    s.master.overrideredirect(1)
    s.master.update_idletasks()
    width, height = s.master.winfo_reqwidth(), s.master.winfo_reqheight()
    if point and position:
      if  position == 'ur': x, y = point[0], point[1] - height
      elif position == 'lr': x, y = point[0], point[1]
      elif position == 'ul': x, y = point[0] - width, point[1] - height
      elif position == 'll': x, y = point[0] - width, point[1]
    else: x, y = (s.master.winfo_screenwidth() - width)/2, (s.master.winfo_screenheight() - height)/2
    s.master.geometry('%dx%d+%d+%d' % (width, height, x, y)) #窗口位置居中
    s.master.after(300, s._main_judge)
    s.master.deiconify()
    s.master.focus_set()
    s.master.wait_window() #这里应该使用wait_window挂起窗口,如果使用mainloop,可能会导致主程序很多错误

  def __get_calendar(s, locale, fwday):
    # 实例化适当的日历类
    if locale is None:
      return calendar.TextCalendar(fwday)
    else:
      return calendar.LocaleTextCalendar(fwday, locale)

  def __setitem__(s, item, value):
    if item in ('year', 'month'):
      raise AttributeError("attribute '%s' is not writeable" % item)
    elif item == 'selectbackground':
      s._canvas['background'] = value
    elif item == 'selectforeground':
      s._canvas.itemconfigure(s._canvas.text, item=value)
    else:
      s.G_Frame.__setitem__(s, item, value)

  def __getitem__(s, item):
    if item in ('year', 'month'):
      return getattr(s._date, item)
    elif item == 'selectbackground':
      return s._canvas['background']
    elif item == 'selectforeground':
      return s._canvas.itemcget(s._canvas.text, 'fill')
    else:
      r = ttk.tclobjs_to_py({item: ttk.Frame.__getitem__(s, item)})
      return r[item]

  def __setup_styles(s):
    # 自定义TTK风格
    style = ttk.Style(s.master)
    arrow_layout = lambda dir: (
      [('Button.focus', {'children': [('Button.%sarrow' % dir, None)]})]
    )
    style.layout('L.TButton', arrow_layout('left'))
    style.layout('R.TButton', arrow_layout('right'))

  def __place_widgets(s):
    # 标头框架及其小部件
    Input_judgment_num = s.master.register(s.Input_judgment) # 需要将函数包装一下,必要的
    hframe = ttk.Frame(s.G_Frame)
    gframe = ttk.Frame(s.G_Frame)
    bframe = ttk.Frame(s.G_Frame)
    hframe.pack(in_=s.G_Frame, side='top', pady=5, anchor='center')
    gframe.pack(in_=s.G_Frame, fill=tk.X, pady=5)
    bframe.pack(in_=s.G_Frame, side='bottom', pady=5)

    lbtn = ttk.Button(hframe, style='L.TButton', command=s._prev_month)
    lbtn.grid(in_=hframe, column=0, row=0, padx=12)
    rbtn = ttk.Button(hframe, style='R.TButton', command=s._next_month)
    rbtn.grid(in_=hframe, column=5, row=0, padx=12)
    
    s.CB_year = ttk.Combobox(hframe, width = 5, values = [str(year) for year in range(datetime.now().year, datetime.now().year-11,-1)], validate = 'key', validatecommand = (Input_judgment_num, '%P'))
    s.CB_year.current(0)
    s.CB_year.grid(in_=hframe, column=1, row=0)
    s.CB_year.bind('<KeyPress>', lambda event:s._update(event, True))
    s.CB_year.bind("<<ComboboxSelected>>", s._update)
    tk.Label(hframe, text = '年', justify = 'left').grid(in_=hframe, column=2, row=0, padx=(0,5))

    s.CB_month = ttk.Combobox(hframe, width = 3, values = ['%02d' % month for month in range(1,13)], state = 'readonly')
    s.CB_month.current(datetime.now().month - 1)
    s.CB_month.grid(in_=hframe, column=3, row=0)
    s.CB_month.bind("<<ComboboxSelected>>", s._update)
    tk.Label(hframe, text = '月', justify = 'left').grid(in_=hframe, column=4, row=0)

    # 日历部件
    s._calendar = ttk.Treeview(gframe, show='', selectmode='none', height=7)
    s._calendar.pack(expand=1, fill='both', side='bottom', padx=5)

    ttk.Button(bframe, text = "确 定", width = 6, command = lambda: s._exit(True)).grid(row = 0, column = 0, sticky = 'ns', padx = 20)
    ttk.Button(bframe, text = "取 消", width = 6, command = s._exit).grid(row = 0, column = 1, sticky = 'ne', padx = 20)
    
    
    tk.Frame(s.G_Frame, bg = '#565656').place(x = 0, y = 0, relx = 0, rely = 0, relwidth = 1, relheigh = 2/200)
    tk.Frame(s.G_Frame, bg = '#565656').place(x = 0, y = 0, relx = 0, rely = 198/200, relwidth = 1, relheigh = 2/200)
    tk.Frame(s.G_Frame, bg = '#565656').place(x = 0, y = 0, relx = 0, rely = 0, relwidth = 2/200, relheigh = 1)
    tk.Frame(s.G_Frame, bg = '#565656').place(x = 0, y = 0, relx = 198/200, rely = 0, relwidth = 2/200, relheigh = 1)

  def __config_calendar(s):
    # cols = s._cal.formatweekheader(3).split()
    cols = ['日','一','二','三','四','五','六']
    s._calendar['columns'] = cols
    s._calendar.tag_configure('header', background='grey90')
    s._calendar.insert('', 'end', values=cols, tag='header')
    # 调整其列宽
    font = tkFont.Font()
    maxwidth = max(font.measure(col) for col in cols)
    for col in cols:
      s._calendar.column(col, width=maxwidth, minwidth=maxwidth,
        anchor='center')

  def __setup_selection(s, sel_bg, sel_fg):
    def __canvas_forget(evt):
      canvas.place_forget()
      s._selection = None

    s._font = tkFont.Font()
    s._canvas = canvas = tk.Canvas(s._calendar, background=sel_bg, borderwidth=0, highlightthickness=0)
    canvas.text = canvas.create_text(0, 0, fill=sel_fg, anchor='w')

    canvas.bind('<Button-1>', __canvas_forget)
    s._calendar.bind('<Configure>', __canvas_forget)
    s._calendar.bind('<Button-1>', s._pressed)

  def _build_calendar(s):
    year, month = s._date.year, s._date.month

    # update header text (Month, YEAR)
    header = s._cal.formatmonthname(year, month, 0)

    # 更新日历显示的日期
    cal = s._cal.monthdayscalendar(year, month)
    for indx, item in enumerate(s._items):
      week = cal[indx] if indx < len(cal) else []
      fmt_week = [('%02d' % day) if day else '' for day in week]
      s._calendar.item(item, values=fmt_week)

  def _show_select(s, text, bbox):
    """为新的选择配置画布。"""
    x, y, width, height = bbox

    textw = s._font.measure(text)

    canvas = s._canvas
    canvas.configure(width = width, height = height)
    canvas.coords(canvas.text, (width - textw)/2, height / 2 - 1)
    canvas.itemconfigure(canvas.text, text=text)
    canvas.place(in_=s._calendar, x=x, y=y)

  def _pressed(s, evt = None, item = None, column = None, widget = None):
    """在日历的某个地方点击。"""
    if not item:
      x, y, widget = evt.x, evt.y, evt.widget
      item = widget.identify_row(y)
      column = widget.identify_column(x)

    if not column or not item in s._items:
      # 在工作日行中单击或仅在列外单击。
      return

    item_values = widget.item(item)['values']
    if not len(item_values): # 这个月的行是空的。
      return

    text = item_values[int(column[1]) - 1]
    if not text: # 日期为空
      return

    bbox = widget.bbox(item, column)
    if not bbox: # 日历尚不可见
      s.master.after(20, lambda : s._pressed(item = item, column = column, widget = widget))
      return

    # 更新,然后显示选择
    text = '%02d' % text
    s._selection = (text, item, column)
    s._show_select(text, bbox)

  def _prev_month(s):
    """更新日历以显示前一个月。"""
    s._canvas.place_forget()
    s._selection = None

    s._date = s._date - timedelta(days=1)
    s._date = datetime(s._date.year, s._date.month, 1)
    s.CB_year.set(s._date.year)
    s.CB_month.set(s._date.month)
    s._update()

  def _next_month(s):
    """更新日历以显示下一个月。"""
    s._canvas.place_forget()
    s._selection = None

    year, month = s._date.year, s._date.month
    s._date = s._date + timedelta(
      days=calendar.monthrange(year, month)[1] + 1)
    s._date = datetime(s._date.year, s._date.month, 1)
    s.CB_year.set(s._date.year)
    s.CB_month.set(s._date.month)
    s._update()

  def _update(s, event = None, key = None):
    """刷新界面"""
    if key and event.keysym != 'Return': return
    year = int(s.CB_year.get())
    month = int(s.CB_month.get())
    if year == 0 or year > 9999: return
    s._canvas.place_forget()
    s._date = datetime(year, month, 1)
    s._build_calendar() # 重建日历

    if year == datetime.now().year and month == datetime.now().month:
      day = datetime.now().day
      for _item, day_list in enumerate(s._cal.monthdayscalendar(year, month)):
        if day in day_list:
          item = 'I00' + str(_item + 2)
          column = '#' + str(day_list.index(day)+1)
          s.master.after(100, lambda :s._pressed(item = item, column = column, widget = s._calendar))

  def _exit(s, confirm = False):
    """退出窗口"""
    if not confirm: s._selection = None
    s.master.destroy()

  def _main_judge(s):
    """判断窗口是否在最顶层"""
    try:
      #s.master 为 TK 窗口
      #if not s.master.focus_displayof(): s._exit()
      #else: s.master.after(10, s._main_judge)

      #s.master 为 toplevel 窗口
      if s.master.focus_displayof() == None or 'toplevel' not in str(s.master.focus_displayof()): s._exit()
      else: s.master.after(10, s._main_judge)
    except:
      s.master.after(10, s._main_judge)

    #s.master.tk_focusFollowsMouse() # 焦点跟随鼠标

  def selection(s):
    """返回表示当前选定日期的日期时间。"""
    if not s._selection: return None

    year, month = s._date.year, s._date.month
    return str(datetime(year, month, int(s._selection[0])))[:10]

  def Input_judgment(s, content):
    """输入判断"""
    # 如果不加上==""的话,就会发现删不完。总会剩下一个数字
    if content.isdigit() or content == "":
      return True
    else:
      return False

if __name__ == '__main__':
  root = tk.Tk()

  width, height = root.winfo_reqwidth() + 50, 50 #窗口大小
  x, y = (root.winfo_screenwidth() - width )/2, (root.winfo_screenheight() - height)/2
  root.geometry('%dx%d+%d+%d' % (width, height, x, y )) #窗口位置居中

  date_str = tk.StringVar()
  date = ttk.Entry(root, textvariable = date_str)
  date.place(x = 0, y = 0, relx = 5/20, rely = 1/6, relwidth = 14/20, relheigh = 2/3)

  #Calendar((x, y), 'ur').selection() 获取日期,x,y为点坐标
  date_str_gain = lambda: [
    date_str.set(date)
    for date in [Calendar((x, y), 'ur').selection()] 
    if date]
  tk.Button(root, text = '日期:', command = date_str_gain).place(x = 0, y = 0, relx = 1/20, rely = 1/6, relwidth = 4/20, relheigh = 2/3)
  root.mainloop()

以上这篇Python tkinter 下拉日历控件代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python获得linux下所有挂载点(mount points)的方法
Apr 29 Python
Python中Iterator迭代器的使用杂谈
Jun 20 Python
Python实现二维数组按照某行或列排序的方法【numpy lexsort】
Sep 22 Python
python ddt数据驱动最简实例代码
Feb 22 Python
对Python 简单串口收发GUI界面的实例详解
Jun 12 Python
PyQt QListWidget修改列表项item的行高方法
Jun 20 Python
如何用Python制作微信好友个性签名词云图
Jun 28 Python
python可视化爬虫界面之天气查询
Jul 03 Python
python numpy 矩阵堆叠实例
Jan 17 Python
解决Pycharm中恢复被exclude的项目问题(pycharm source root)
Feb 14 Python
python opencv pytesseract 验证码识别的实现
Aug 28 Python
Python OpenCV实现图形检测示例详解
Apr 08 Python
Python Tkinter Entry和Text的添加与使用详解
Mar 04 #Python
Python列表倒序输出及其效率详解
Mar 04 #Python
python tkinter之顶层菜单、弹出菜单实例
Mar 04 #Python
python tkinter之 复选、文本、下拉的实现
Mar 04 #Python
自定义Django默认的sitemap站点地图样式
Mar 04 #Python
Python 窗体(tkinter)下拉列表框(Combobox)实例
Mar 04 #Python
Python3中的f-Strings增强版字符串格式化方法
Mar 04 #Python
You might like
php中实现简单的ACL 完结篇
2011/09/07 PHP
Yii框架通过请求组件处理get,post请求的方法分析
2019/09/03 PHP
扩展Jquery插件处理mouseover时内部有子元素时发生样式闪烁
2011/12/08 Javascript
使用javascript实现雪花飘落的效果
2015/01/13 Javascript
JavaScript生成SQL查询表单的方法
2015/08/13 Javascript
JS中JSON对象和String之间的互转及处理技巧
2016/04/06 Javascript
javascript回到顶部特效
2016/07/30 Javascript
NodeJS学习笔记之Module的简介
2017/03/24 NodeJs
JavaScript实现获取用户单击body中所有A标签内容的方法
2017/06/05 Javascript
React进阶学习之组件的解耦之道
2017/08/07 Javascript
JavaScript实现的搜索及高亮显示功能示例
2017/08/14 Javascript
JS实现快速比较两个字符串中包含有相同数字的方法
2017/09/11 Javascript
Mac中安装nvm的教程分享
2017/12/11 Javascript
vue实现图片加载完成前的loading组件方法
2018/02/05 Javascript
Python实现的单向循环链表功能示例
2017/11/10 Python
使用实现XlsxWriter创建Excel文件并编辑
2018/05/04 Python
Python匿名函数/排序函数/过滤函数/映射函数/递归/二分法
2019/06/05 Python
python+pygame实现坦克大战
2019/09/10 Python
Django+uni-app实现数据通信中的请求跨域的示例代码
2019/10/12 Python
使用遗传算法求二元函数的最小值
2020/02/11 Python
惠普墨西哥官方商店:HP墨西哥
2016/12/01 全球购物
阿迪达斯墨西哥官方网站:adidas墨西哥
2017/11/03 全球购物
美国孕妇装购物网站:Motherhood Maternity
2019/09/22 全球购物
煤矿班组长的职责
2013/12/25 职场文书
计算机相关的自我评价
2014/01/15 职场文书
文明青少年标兵事迹材料
2014/01/28 职场文书
试用期自我鉴定范文
2014/03/20 职场文书
区域销售主管岗位职责
2014/06/15 职场文书
就业协议书盖章的注意事项
2014/09/28 职场文书
初中作文评语集锦
2014/12/25 职场文书
2015年读书月活动总结
2015/03/26 职场文书
婚宴父母致辞
2015/07/27 职场文书
学习雷锋主题班会
2015/08/14 职场文书
不会写演讲稿,快来看看这篇文章!
2019/08/06 职场文书
教你用Python写一个植物大战僵尸小游戏
2021/04/25 Python
默认网关不可用修复后过一会又不好使了解决方法
2022/04/08 数码科技