3

I am trying to create a drop down calendar for a date entry. Below is a portion of my code:

The drop down portion of it dosen't work and I can't seem to find the syntax for DateEntry() of ttk calendar anywhere to include the calendar widget option!

#creating the frame 
from tkinter import *
from tkcalendar import *

root = Tk()

f1=Frame(root,width=1500,height=100,relief=SUNKEN,bd=4,bg='light steel blue')
f1.pack(side=TOP)
f2=Frame(root,width=1500,height=550,relief=SUNKEN,bd=4,bg='white')
f2.pack()
f3=Frame(root,width=1600,height=100,relief=SUNKEN,bd=4,bg='white')
f3.pack(side=BOTTOM)


#Creating the date column
l4=Label(f2,text='DATE',font=('tahoma',20,'bold'),fg='black',anchor='w')
l4.grid(row=0,column=3)

cal=DateEntry(f2,dateformat=3,width=12, background='darkblue',
                    foreground='white', borderwidth=4,Calendar =2018)
cal.grid(row=1,column=3,sticky='nsew')

I want it to look like this: screenshot

j_4321
  • 15,431
  • 3
  • 34
  • 61
user9093127
  • 181
  • 2
  • 3
  • 12
  • There is a problem with the code you provide, you are using `root` (in `f1=..` before defining it) – j_4321 Jan 17 '18 at 10:10
  • In addition, I don't understand what you want to achieve exactly. Are you talking about https://github.com/j4321/tkcalendar? – j_4321 Jan 17 '18 at 10:12
  • @j_4321 apologies manual error . My issue is , i am trying to create a calendar dropdown where once the user clicks it ,there is a small calendar drop down and he is able to select the date ; The selected date then appears in the space.My issue is with this line of code cal=DateEntry(f2,dateformat=3,width=12, background='darkblue', foreground='white', borderwidth=4,Calendar =2018) – user9093127 Jan 17 '18 at 10:17
  • concerning syntax, check https://pypi.python.org/pypi/tkcalendar#Documentation – R4PH43L Jan 17 '18 at 10:22
  • `DateEntry` has no keyword `Calendar`, just directly pass the calendar keywords. – j_4321 Jan 17 '18 at 10:25
  • @j_4321yes ive viewed the page ,not sure how i go about doing this "just directly pass the calendar keywords" – user9093127 Jan 17 '18 at 11:02
  • Can you tell me exactly what option you want to pass to the widget? – j_4321 Jan 17 '18 at 12:03
  • i want it to look like this :https://cdn.ablebits.com/_img-blog/excel-calendar/dropdown-calendar-excel.png @j_4321 my comment merely fetches the current date but i cant seem to get the calendar to appear below it – user9093127 Jan 17 '18 at 12:11
  • "directly" means you can use the same keywords as in Calendar - ie. `Calendar( ..., year=2018)` and `DateEntry( ... year=2018)` – furas Jan 17 '18 at 12:43
  • I don't understand where the problem is. Except for the line displaying today's date, `DateEntry` gives a result very similar to the one in the picture, the calendar appears when the user clicks on the arrow button. – j_4321 Jan 17 '18 at 12:56
  • @j_4321 exactly thats where the problem is the arrow is not clickable – user9093127 Jan 18 '18 at 05:10
  • @furas yep tries it like cal5=DateEntry(f2,dateformat=3,width=12, background='darkblue', foreground='white', borderwidth=4) ; problem is the drop down isnt actionable – user9093127 Jan 18 '18 at 05:28

1 Answers1

5

UPDATE: I have fixed the issue and published a new version of tkcalendar.

EDIT: the problem is that in Windows, the drop-down does not open when the downarrow button is clicked. It seems that it comes from the default ttk theme for Windows because it works with other themes. So the workaround is to switch theme and use 'clam' for instance ('alt' should work as well). Meanwhile, I will look into it and see if I can fix the DateEntry for the other themes and release a new version (https://github.com/j4321/tkcalendar/issues/3).

I am not sure what you want to achieve exactly with the DateEntry, but if your goal is to make it look like the one in the picture, it can be done the following way:

import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry
from datetime import date

root = tk.Tk()
# change ttk theme to 'clam' to fix issue with downarrow button
style = ttk.Style(root)
style.theme_use('clam')

class MyDateEntry(DateEntry):
    def __init__(self, master=None, **kw):
        DateEntry.__init__(self, master=None, **kw)
        # add black border around drop-down calendar
        self._top_cal.configure(bg='black', bd=1)
        # add label displaying today's date below
        tk.Label(self._top_cal, bg='gray90', anchor='w',
                 text='Today: %s' % date.today().strftime('%x')).pack(fill='x')

# create the entry and configure the calendar colors
de = MyDateEntry(root, year=2016, month=9, day=6,
                 selectbackground='gray80',
                 selectforeground='black',
                 normalbackground='white',
                 normalforeground='black',
                 background='gray90',
                 foreground='black',
                 bordercolor='gray90',
                 othermonthforeground='gray50',
                 othermonthbackground='white',
                 othermonthweforeground='gray50',
                 othermonthwebackground='white',
                 weekendbackground='white',
                 weekendforeground='black',
                 headersbackground='white',
                 headersforeground='gray70')
de.pack()
root.mainloop()

I created a class inheriting from DateEntry to add the label with today's date below the calendar and to create a black border around the drop-down (self._top_cal is the Toplevel containing the calendar).

Then, I created an instance of MyDateEntry and with all calendar options needed to make it look like the picture. In addition, I used the year, month, day options to define the initial date inside the entry. Here is the result:

screenshot

j_4321
  • 15,431
  • 3
  • 34
  • 61
  • So Ive compiled the code and all i'm getting is a date entry box with the drop down unactionable . Am i missing something here ? is there a bug in python 3.4.0 ?? not sure what the prob is :( – user9093127 Jan 18 '18 at 05:45
  • @user9093127 I am using python 3.6 and tcl/tk 8.6, so this might be a compatibility issue. I am using appveyor and travis-ci to check the compatibility for older python (including python 3.4) but I don't remember if I have written a test for the button. What OS are you using and with which version of tk? – j_4321 Jan 18 '18 at 08:35
  • m using python 3.4.0 and tk8.6 ;windows os – user9093127 Jan 18 '18 at 08:48
  • I have tested the `DateEntry` with python 3.3.7 and 3.4.7 in linux and it works. I don't have windows available right now, but I will make some tests when I can. – j_4321 Jan 18 '18 at 09:13
  • @user9093127 I think I have identified the issue, it is Windows default ttk theme which is less flexible than others and does not seems to handle the downarrow button right. So for now, the solution is to change theme (I have updated the answer) and I will see if I can fix the problem with Windows theme. – j_4321 Jan 18 '18 at 09:55
  • @user9093127 I have fixed the issue on WIndows and published a new release. – j_4321 Jan 25 '18 at 08:42
  • @j_4321 : tkcalendar, DateEntry is not working for me. On trying to install it using pip install tkcalendar its throwing an error ("No matching distribution found for tkcalendar") – Garima Tiwari May 21 '18 at 14:50
  • @GarimaTiwari which OS and python version are you using? – j_4321 May 22 '18 at 07:21
  • @j_4321 : I am using Python 3.6.1 on Windows7 – Garima Tiwari May 22 '18 at 09:35
  • @GarimaTiwari I don't have access to Windows to test it right now but pip works for me with python3.6 on Linux. Please open a bug report on https://github.com/j4321/tkcalendar/issues and I'll see what I can do. – j_4321 May 22 '18 at 09:43
  • @j_4321 : I was able to install tkcalendar finally! there seemed to be some restriction in terms of the packages we could download earlier. – Garima Tiwari May 31 '18 at 13:58
  • @j_4321 : I do however have another question in relation to DateEntry please - can you have a look at this please? Many many thanks https://stackoverflow.com/questions/50625818/how-to-get-the-selected-date-for-dateentry-in-tkcalendar-python – Garima Tiwari May 31 '18 at 13:59