python - 如何在 tkcalendar 中获取单元格的背景?
问题描述
我有这个日历,我想获取所选单元格的背景颜色,我该怎么做?
Current_Date = str(datetime.today())
Current_Date=Current_Date[0:10].split('-')
cal = Calendar(root, selectmode="day", year=int(Current_Date[0]),
month=int(Current_Date[1]), day=int(Current_Date[2]))
cal.pack(pady=20)
解决方案
坦率地说,我不知道您为什么需要访问单元格以及为什么需要了解单元格的背景。如果你想改变单元格的颜色,那么你应该使用标签`
顺便说一句:您不必将日期转换为字符串并将其拆分
current_date = datetime.datetime.today()
cal = tkcalendar.Calendar(root, selectmode="day",
year=current_date.year,
month=current_date.month,
day=current_date.day)
tkcalendar
不能直接访问单元格。但是隐藏cal._calendar
了所有用于创建单元格的标签 - 这样您就可以尝试访问您需要的标签。但是 Label 不直接使用,background
但是style
您必须将样式转换为免费颜色。
此代码显示当月所有单元格的样式
import datetime
import tkinter as tk
import tkcalendar
# --- functions
def on_click():
#print(cal._calendar)
# display all `labels`
for row in cal._calendar:
for day in row:
number = day['text']
style = day['style']
background = cal.style.configure(day['style'])['background']
print('day: {:2} | style: {:27} | background: {}'.format(number, style, background))
# --- main ---
root = tk.Tk()
current_date = datetime.datetime.today() # PEP8: lower_case_namas
cal = tkcalendar.Calendar(root, selectmode="day",
year=current_date.year,
month=current_date.month,
day=current_date.day)
cal.pack(pady=20, padx=20)
button = tk.Button(root, text='Show all styles', command=on_click)
button.pack(pady=(0,20), padx=20)
print(cal.style)
root.mainloop()
结果:
day: 30 | style: normal_om..!calendar.TLabel | background: gray93
day: 1 | style: normal..!calendar.TLabel | background: white
day: 2 | style: normal..!calendar.TLabel | background: white
day: 3 | style: normal..!calendar.TLabel | background: white
day: 4 | style: normal..!calendar.TLabel | background: white
day: 5 | style: we..!calendar.TLabel | background: gray80
day: 6 | style: we..!calendar.TLabel | background: gray80
day: 7 | style: normal..!calendar.TLabel | background: white
day: 8 | style: normal..!calendar.TLabel | background: white
day: 9 | style: normal..!calendar.TLabel | background: white
day: 10 | style: normal..!calendar.TLabel | background: white
[...]
如果您想在单击单元格时访问,那么您可能必须创建自己的类并覆盖方法_on_click
import datetime
import tkinter as tk
import tkcalendar
# --- functions
class MyCalendar(tkcalendar.Calendar):
def _on_click(self, event):
print('LABEL:', event.widget)
print('LABEL text :', event.widget['text'])
print('LABEL style:', event.widget['style'])
background = cal.style.configure(event.widget['style'])['background']
print('LABEL background:', background)
print('---')
# run original `_on_click`
super()._on_click(event)
# --- main ---
root = tk.Tk()
current_date = datetime.datetime.today() # PEP8: lower_case_namas
cal = MyCalendar(root, selectmode="day",
year=current_date.year,
month=current_date.month,
day=current_date.day)
cal.pack(pady=20, padx=20)
cal.bind('<<CalendarSelected>>', on_select)
root.mainloop()
结果(单击日历中的某些日期时):
LABEL: .!mycalendar.!frame2.!label29
LABEL text : 17
LABEL style: normal..!mycalendar.TLabel
LABEL background: white
---
LABEL: .!mycalendar.!frame2.!label32
LABEL text : 20
LABEL style: we..!mycalendar.TLabel
LABEL background: gray80
---
推荐阅读
- angular - 如何在Angular中的组件顶部显示微调器
- python - ImportError numpy.core.multiarray 导入失败
- azure - 找不到模块:'Az.Accounts' - 在 VM 规模上设置 Azure Windows 数据中心 2019
- apache-spark - 通过sqlquery读取大表时pyspark无法生成作业
- matplotlib - 在 seaborn 中,如何关闭 xaxis 标题,但保留 xaxis 标签和刻度?
- r - 排序和删除元素后如何恢复向量的原始顺序?
- graphql - 无法连接到 WebSocket 端点 [apollo-server-express with GooglePubSub]
- javascript - 使用 vue 公式上传多张图片
- r-markdown - 4 级和 5 级标题未包含在 officedown 的目录中
- javascript - 如何找出具有相似类的块划掉固定块(getBoundingClientRect)?