最近在写一个小应用,需要获取当月多个日期当数据依据,找了一圈没有找到可以实现多选的代码,可单选的代码倒是很多,看了一圈,最后参考了这位叫 我的眼_001的博主的代码,在此对博主表示感谢(Python tkinter 下拉日历控件 作者:我的眼_001)。
最后效果代码实现
# -*- codeing = utf-8 -*-
# Author : Abner
# @Software : PyCharm
import tkinter as tk
from tkinter import ttk
from calendar import Calendar
from time import strptime
class CalendarMultiSelection:
def __init__(self):
self.dates_month = list() # 此列表用于存储当月所有的日期
self.check_status = list() # 此列表用于存储日期选中状态
self.win = tk.Tk()
self.win.geometry('330x400')
self.win.resizable(False, False)
# 顶框架----------------------------
self.frame_top = tk.Frame(self.win)
self.frame_top.pack(side='top', padx=5, pady=5)
self.label_calendar_top = tk.Frame(self.frame_top)
self.label_calendar_top.pack()
self.label_calendar_select = tk.Frame(self.frame_top)
self.label_calendar_select.pack(pady=5)
#
self.year_month()
self.calendar_select()
# 底框架------------------------------
self.frame_bottom = tk.Frame(self.win)
self.frame_bottom.pack(side='bottom', padx=5, pady=5)
#
self.submit_button()
self.win.mainloop()
# 顶部框架内容----------------------------
def year_month(self):
l_year = tk.Label(self.label_calendar_top, text='请选择年份:')
l_year.grid(row=1, column=1)
self.combox_year = ttk.Combobox(self.label_calendar_top, width=10, state='readonly')
self.combox_year['value'] = [x for x in range(2010, 2030)]
self.combox_year.current(12)
self.combox_year.grid(row=1, column=2, padx=5)
l_month = tk.Label(self.label_calendar_top, text='请选择月份:')
l_month.grid(row=1, column=3)
self.combox_month = ttk.Combobox(self.label_calendar_top, width=5, state='readonly')
self.combox_month['value'] = [x + 1 for x in range(12)]
self.combox_month.current(1)
self.combox_month.grid(row=1, column=4, padx=5)
self.combox_year.bind('<>', self.calendar_select) # 下拉框绑定函数,更改年月触发函数重新生成日历
self.combox_month.bind('<>', self.calendar_select)
def calendar_select(self, *args):
self.year = int(self.combox_year.get()) # 获取下拉框文本并转为整型
self.month = int(self.combox_month.get())
for i in self.label_calendar_select.winfo_children(): # 清除框架刷新数据
i.destroy()
self.check_status.clear()
self.dates_month = [da.strftime('%Y-%m-%d') for da in (Calendar().itermonthdates(self.year, self.month))] # 获取当月所有日期
columns = ['一', '二', '三', '四', '五', '六', '日']
for i in range(len(columns)): # 生成抬头
tk.Label(self.label_calendar_select, text=columns[i], width=5, anchor='center',
relief='groove').grid(row=1, column=i, sticky='w')
for index, dates in enumerate(self.dates_month): # 用复选框来解决多选的问题
self.check_status.append(tk.IntVar()) # 选中状态(0/1)
if strptime(dates, '%Y-%m-%d').tm_mon != self.month: # 排除掉非本月的日期
dates = ''
else:
date = strptime(dates, '%Y-%m-%d').tm_mday
check_box = tk.Checkbutton(self.label_calendar_select, text=date,
command=lambda arg=dates: print(arg))
check_box.configure(width=4, variable=self.check_status[-1], indicatoron=False,
selectcolor='red', onvalue=1, offvalue=0)
check_box.grid(row=index // 7 + 2, column=index % 7) # 置放位置
# 底部框架内容--------------------
def submit_button(self):
button = tk.Button(self.frame_bottom, text='提交', width=15, command=self.submit_data)
button.pack(padx=5, pady=5)
def submit_data(self):
date_select_statu = [x.get() for x in self.check_status]
dates_selected = dict(zip(self.dates_month, date_select_statu))
self.dates = list({k: v for k, v in dates_selected.items() if v == 1}.keys())
print('选中日期:', self.dates)
if __name__ == '__main__':
CalendarMultiSelection()
新手练习,如有不足或错误请指正,感激不尽。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)