__main__.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from tkcalendar import Calendar, DateEntry
  2. try:
  3. import tkinter as tk
  4. from tkinter import ttk
  5. except ImportError:
  6. import Tkinter as tk
  7. import ttk
  8. def example1():
  9. def print_sel():
  10. print(cal.selection_get())
  11. cal.see(datetime.date(year=2016, month=2, day=5))
  12. top = tk.Toplevel(root)
  13. import datetime
  14. today = datetime.date.today()
  15. mindate = datetime.date(year=2018, month=1, day=21)
  16. maxdate = today + datetime.timedelta(days=5)
  17. print(mindate, maxdate)
  18. cal = Calendar(top, font="Arial 14", selectmode='day', locale='en_US',
  19. mindate=mindate, maxdate=maxdate, disabledforeground='red',
  20. cursor="hand1", year=2018, month=2, day=5)
  21. cal.pack(fill="both", expand=True)
  22. ttk.Button(top, text="ok", command=print_sel).pack()
  23. def example2():
  24. top = tk.Toplevel(root)
  25. cal = Calendar(top, selectmode='none')
  26. date = cal.datetime.today() + cal.timedelta(days=2)
  27. cal.calevent_create(date, 'Hello World', 'message')
  28. cal.calevent_create(date, 'Reminder 2', 'reminder')
  29. cal.calevent_create(date + cal.timedelta(days=-2), 'Reminder 1', 'reminder')
  30. cal.calevent_create(date + cal.timedelta(days=3), 'Message', 'message')
  31. cal.tag_config('reminder', background='red', foreground='yellow')
  32. cal.pack(fill="both", expand=True)
  33. ttk.Label(top, text="Hover over the events.").pack()
  34. def example3():
  35. top = tk.Toplevel(root)
  36. ttk.Label(top, text='Choose date').pack(padx=10, pady=10)
  37. cal = DateEntry(top, width=12, background='darkblue',
  38. foreground='white', borderwidth=2, year=2010)
  39. cal.pack(padx=10, pady=10)
  40. root = tk.Tk()
  41. ttk.Button(root, text='Calendar', command=example1).pack(padx=10, pady=10)
  42. ttk.Button(root, text='Calendar with events', command=example2).pack(padx=10, pady=10)
  43. ttk.Button(root, text='DateEntry', command=example3).pack(padx=10, pady=10)
  44. root.mainloop()