Moe*_*Jan 6 python tkinter ttk
我在我的应用程序中使用此ttk日历.
我想要做的是使用datetime.date实例设置日历,以便在日历出现时突出显示指定的日期.
我以为我可以通过_show_selection手动text和bboxargs 来完成这个方法.为了测试这个想法,我将这一行放在__init__方法的末尾:
self._show_selection('%02d'%16,(42,61,41,20))
我希望它能突出本月16日(5月),但事实并非如此.
我print text, bbox在_pressed方法中运行了args .
如果有人能够对此有所了解,我将非常感激.
在 __setup_selection() 中,有一个对配置事件的绑定。据推测,它是为了在调整日历大小时删除“选择画布”。但是,当日历首次映射到屏幕时,配置事件也会触发,因此您预先选择的日期在您看到它之前就消失了。
set_day(如下)允许您以编程方式选择一天。如果小部件尚不可见,它会通过重新调度自身来回避第一个配置事件的问题。
日历更改:
def __setup_selection(self, sel_bg, sel_fg):
self._font = font.Font()
self._canvas = canvas = tkinter.Canvas(self._calendar,
background=sel_bg, borderwidth=0, highlightthickness=0)
canvas.text = canvas.create_text(0, 0, fill=sel_fg, anchor='w')
canvas.bind('<ButtonPress-1>', lambda evt: canvas.place_forget())
#self._calendar.bind('<Configure>', lambda evt: canvas.place_forget())
self._calendar.bind('<Configure>', self.on_configure)
self._calendar.bind('<ButtonPress-1>', self._pressed)
def on_configure(self, event):
self._canvas.place_forget()
if self._selection is not None:
text, iid, column = self._selection
bbox = self._calendar.bbox(iid, column)
self._show_selection(text, bbox)
def _prev_month(self):
"""Updated calendar to show the previous month."""
self._canvas.place_forget()
self._selection = None #
self._date = self._date - self.timedelta(days=1)
self._date = self.datetime(self._date.year, self._date.month, 1)
self._build_calendar() # reconstuct calendar
def _next_month(self):
"""Update calendar to show the next month."""
self._canvas.place_forget()
self._selection = None #
year, month = self._date.year, self._date.month
self._date = self._date + self.timedelta(
days=calendar.monthrange(year, month)[1] + 1)
self._date = self.datetime(self._date.year, self._date.month, 1)
self._build_calendar() # reconstruct calendar
def set_day(self, day):
w = self._calendar
if not w.winfo_viewable():
w.after(200, self.set_day, day)
return
text = '%02d' % day
column = None
for iid in self._items:
rowvals = w.item(iid, 'values')
try:
column = rowvals.index(text)
except ValueError as err:
pass
else:
item = iid
bbox = w.bbox(iid, column)
break
if column is not None:
self._selection = (text, item, column)
self._show_selection(text, bbox)
#test
def test():
import sys
root = tkinter.Tk()
root.title('Ttk Calendar')
ttkcal = Calendar(firstweekday=calendar.SUNDAY)
ttkcal.pack(expand=1, fill='both')
if 'win' not in sys.platform:
style = ttk.Style()
style.theme_use('clam')
ttkcal.set_day(16) #
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
416 次 |
| 最近记录: |