L
L
Ledauph12021-07-19 01:00:48
Python
Ledauph1, 2021-07-19 01:00:48

How to set a dynamically changing date range in tkcalendar?

There are two DateEntries. It is necessary when selecting a date in DateEntry_1, change mindate(minimum valid date) in DateEntry_2. Preferably without using additional widgets such as buttons.
Example: the date selected in DateEntry_1 is 10/12/2021, the date available in DateEntry_2 is 10/12/2021 and beyond.
To get the selected date in DateEntry_1 I use an event binding (bind):

spoiler
def get_data_now(self):
        def print_sel(e):
            print(self.cal.get_date())
            return self.cal.get_date()
        self.cal.bind("<<DateEntrySelected>>", print_sel)


the code
import tkinter as tk
from tkinter import ttk
import datetime
from tkcalendar import Calendar, DateEntry



class Date_Picker:
    def __init__(self, master):
        self.root = master
        self.label = ttk.Label(self.root, text='Select date').pack(side='left')
        self.cal = DateEntry(self.root, width=12, background='darkblue',
                        foreground='white', borderwidth=2, date_pattern='dd/mm/YYYY')
        self.cal.pack(side='left')
    def get_data_now(self):
        def print_sel(e):
            print(self.cal.get_date())
            return self.cal.get_date()
        self.cal.bind("<<DateEntrySelected>>", print_sel)
    def show(self):
        print('Выбраная дата: ', self.cal.get())
        return self.cal.get()

root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')
cal_form = Date_Picker(root)
cal_to = Date_Picker(root)


root.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EgorSvinarev, 2021-07-19
@Ledauph1

Hello. You can specify the textvariable parameter when initializing the DateEntry, and add a handler for the change event of this variable. Then, in the handler,
change the date through the set_date method.

from tkinter import *
from tkcalendar import DateEntry

root = Tk()

def handler(*args):
  # set data in datetime.date format



variable = StringVar()

entry_1 = DateEntry(root, date_pattern='dd/mm/YYYY', textvariable = variable)
entry_2 = DateEntry(root, date_pattern='dd/mm/YYYY')

variable.trace_add("write", handler)

entry_1.pack()
entry_2.pack()


root.mainloop()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question