在日历上画一些日子
Paint some days in calendar
我创建了一个显示日历的程序。我正在使用 tkinter 和日历导入。
是否可以画出所有星期天的数字或背景(实际上是标记)?
如果您使用 tkcalendar
库,则可以选择配置周末的 background
和 forground
颜色。
一个周末由周六和周日组成。 这意味着您不能只配置 Sundays
而不是两天 .
配置了以下属性:
weekendbackground
- 配置背景颜色
周末。
weekendforeground
- 配置
的前景色
周末。
一个简单的工作代码:
import tkinter as tk
import tkcalendar
root = tk.Tk()
cal = tkcalendar.Calendar(root, selectmode='day', year=2020, month=1, day=1)
cal.config(weekendbackground='blue', weekendforeground='white')
cal.grid(row=0, column=0)
root.mainloop()
截图:
如果您想使用默认颜色,则可以删除这些属性的使用。
您可以在 link - https://pypi.org/project/tkcalendar/
中阅读有关 tkcalendar
的更多信息
希望对您有所帮助!
正如评论中所建议的那样,您可以使用标签来标记星期日。您使用 calendar.month(year, month, 2, 1)
显示日历,因此您的列为两个字符宽并由 space 分隔,因此星期日列从字符 18 开始。
第一行是月份名称,第二行是工作日,因此您需要在第 3 行开始添加标签。
代码如下:
import calendar
from tkinter import Text
text = Text()
text.pack()
# configure tag to change the background and foreground of sundays
text.tag_configure("sunday", background='light grey', foreground='red')
# display calendar
month = calendar.month(2020, 2, 2, 1)
text.insert('1.0', month)
# add the sunday tag to sundays
for line in range(3, len(month.splitlines()) + 1):
text.tag_add('sunday', f"{line}.18", f"{line}.20")
顺便说一下,如果您希望您的日历与直列很好地对齐,就像我的屏幕截图中那样,您需要使用单色spaced 字体。
我创建了一个显示日历的程序。我正在使用 tkinter 和日历导入。 是否可以画出所有星期天的数字或背景(实际上是标记)?
如果您使用 tkcalendar
库,则可以选择配置周末的 background
和 forground
颜色。
一个周末由周六和周日组成。 这意味着您不能只配置 Sundays
而不是两天 .
配置了以下属性:
weekendbackground
- 配置背景颜色 周末。weekendforeground
- 配置
的前景色 周末。
一个简单的工作代码:
import tkinter as tk
import tkcalendar
root = tk.Tk()
cal = tkcalendar.Calendar(root, selectmode='day', year=2020, month=1, day=1)
cal.config(weekendbackground='blue', weekendforeground='white')
cal.grid(row=0, column=0)
root.mainloop()
截图:
如果您想使用默认颜色,则可以删除这些属性的使用。
您可以在 link - https://pypi.org/project/tkcalendar/
中阅读有关tkcalendar
的更多信息
希望对您有所帮助!
正如评论中所建议的那样,您可以使用标签来标记星期日。您使用 calendar.month(year, month, 2, 1)
显示日历,因此您的列为两个字符宽并由 space 分隔,因此星期日列从字符 18 开始。
第一行是月份名称,第二行是工作日,因此您需要在第 3 行开始添加标签。
代码如下:
import calendar
from tkinter import Text
text = Text()
text.pack()
# configure tag to change the background and foreground of sundays
text.tag_configure("sunday", background='light grey', foreground='red')
# display calendar
month = calendar.month(2020, 2, 2, 1)
text.insert('1.0', month)
# add the sunday tag to sundays
for line in range(3, len(month.splitlines()) + 1):
text.tag_add('sunday', f"{line}.18", f"{line}.20")
顺便说一下,如果您希望您的日历与直列很好地对齐,就像我的屏幕截图中那样,您需要使用单色spaced 字体。