Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a date picker in tkinter?

Tags:

python

tkinter

Is there any standard way tkinter apps allow the user to choose a date?

like image 668
MKaras Avatar asked Dec 14 '10 20:12

MKaras


People also ask

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.

What is use of the Mainloop () in Tkinter?

mainloop() tells Python to run the Tkinter event loop. This method listens for events, such as button clicks or keypresses, and blocks any code that comes after it from running until you close the window where you called the method.


1 Answers

In case anyone still needs this - here's a simple code to create a Calendar and DateEntry widget using tkcalendar package.

pip install tkcalendar (for installing the package)

try:     import tkinter as tk     from tkinter import ttk except ImportError:     import Tkinter as tk     import ttk  from tkcalendar import Calendar, DateEntry  def example1():     def print_sel():         print(cal.selection_get())      top = tk.Toplevel(root)      cal = Calendar(top,                    font="Arial 14", selectmode='day',                    cursor="hand1", year=2018, month=2, day=5)     cal.pack(fill="both", expand=True)     ttk.Button(top, text="ok", command=print_sel).pack()  def example2():     top = tk.Toplevel(root)      ttk.Label(top, text='Choose date').pack(padx=10, pady=10)      cal = DateEntry(top, width=12, background='darkblue',                     foreground='white', borderwidth=2)     cal.pack(padx=10, pady=10)  root = tk.Tk() s = ttk.Style(root) s.theme_use('clam')  ttk.Button(root, text='Calendar', command=example1).pack(padx=10, pady=10) ttk.Button(root, text='DateEntry', command=example2).pack(padx=10, pady=10)  root.mainloop() 
like image 123
Garima Tiwari Avatar answered Sep 18 '22 09:09

Garima Tiwari