2

What I want is to be free to choose the date as long as I am on the dateEntry widget (even if i click on down arrow this one won't be detroyed) and destroy this last (dateEntry) if I click some where else.

The problem is tkcalender is made by multiple widgets that's why the focusOut event is set just on one of them.

from tkinter import *
from tkcalendar import DateEntry

def ok(e):
    print(cal.get_date())


root = Tk()
cal = DateEntry(root, year=2010)
cal.pack(padx=10, pady=10)
cal.bind('<FocusOut>', lambda e: cal.destroy())
cal.bind('<Return>', ok)  # validate with Enter
cal.focus_set()

root.mainloop()

If you run the code then you click on the arrow of the DateEntry this one is destroyed and I want this one to stay there until you click some where else in the window to be destroyed.

j_4321
  • 15,431
  • 3
  • 34
  • 61
Ali
  • 687
  • 8
  • 27
  • Do you mean you want the popup with dates stay until you pick a date, and does not destroy itself when it loses focus? – Henry Yik May 20 '19 at 09:19
  • i want it to be destroyed just when you click some where else , if you run the code bellow and click on the arrow this one will be destroyed. sorry for my english i know im bad @HenryYik – Ali May 20 '19 at 09:24
  • you can see [source code of DateEntry](https://tkcalendar.readthedocs.io/en/stable/_modules/tkcalendar/dateentry.html#DateEntry). If you bind to `cal._top_cal.bind()` then arrow shows calendar but it destroys when you click in date in Entry so it is still not good soluton. – furas May 20 '19 at 09:30
  • no that did not help @furas – Ali May 20 '19 at 09:43
  • @HenryYik your solution helped me a lot thanx plz if you can post here again i will colse this – Ali May 21 '19 at 14:01
  • @ALIBENALI sure, glad you found it useful! – Henry Yik May 21 '19 at 14:28

1 Answers1

2

If I understand you correctly, you want the DateEntry not to be destroyed when you click open the calendar. That is doable by checking your current focus and pass if the current focus is a Calendar object.

import tkinter as tk
from tkcalendar import DateEntry, Calendar

def check_focus(event):
    current = root.focus_get()
    if not isinstance(current,Calendar):
        cal.destroy()

root = tk.Tk()

cal = DateEntry(root, year=2010)
cal.pack(padx=10, pady=10)
cal.bind('<FocusOut>', check_focus)
tk.Button(root,text="Click").pack()

root.mainloop()

Try pressing tab to change focus and see.

Henry Yik
  • 22,275
  • 4
  • 18
  • 40