Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ttk calendar programmatically

I am using this ttk calendar in my application.

What I want to do is set the calendar using a datetime.date instance so when the calendar appears the specified date is highlighted.

I thought I could go through the _show_selection method with manual text and bbox args. To test this idea, I placed this line at the end of the __init__ method:

self._show_selection('%02d'%16,(42,61,41,20))

I was hoping it would highlight the 16th of this month (May), but it did not.

I got the args from running print text, bbox in the _pressed method.

If anyone can shed some light on this, I'd greatly appreciate it.

like image 338
Moe Jan Avatar asked May 11 '16 15:05

Moe Jan


People also ask

How do I change the date format in Tkcalendar?

Changing the date format To change the return date format we used get_date(), the get_date() method return the selected date as a datetime. date instance. So we can change the format by using strftime(). In above code ( Button Click to display ) the changes inside the function my_upd() is here.

What is Tkcalendar?

tkcalendar is a python module that provides the Calendar and DateEntry widgets for Tkinter. The DateEntry widget is similar to a Combobox, but the drop-down is not a list but a Calendar to select a date. Events can be displayed in the Calendar with custom colors and a tooltip displays the event list for a given day.


1 Answers

For the most part @Oblivion is correct, however I want to be able to use a Datetime.date object and to be able to traverse the months and years if needed. Once I changed the set_day method below everything worked perfectly.

def set_day(self, dt_object):
    day = dt_object.day
    w = self._calendar

    if not w.winfo_viewable():
        w.after(200, self.set_day, dt_object)
        return
    while dt_object.year < self._date.year:
        self._prev_month()
    while dt_object.year > self._date.year:
        self._next_month()
    while dt_object.month < self._date.month:
        self._prev_month()
    while dt_object.month > self._date.month:
        self._next_month()
    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)
    else:
        print "Column is None" 
like image 118
Moe Jan Avatar answered Sep 19 '22 09:09

Moe Jan