So what I'm trying to do is to make a calendar frame, just like the view of google calendar or any other calendar program really, where I can select a day and see the events I have to do on that day and it's time using python tkinter. Currently on my database I have events with date, starting time and ending time. Note that I am using python 3. Can anyone please give me a clue for how to do it or even send a link to a website where it says how to do it. But please, I'm not searching for a date picker just a calendar showing me what thing I should do when. Thank you ;)
Asked
Active
Viewed 6,724 times
-5
-
1Please refer to [asking](https://stackoverflow.com/help/asking). – Nae Dec 23 '17 at 17:49
-
1Either learn tkinter and you will know how to do it, or learn some HTML and CSS and use a Python web framework (such as flask or django, or many others) to build it the web way. There are entire books on both paths, but also documentation in the web. – progmatico Dec 23 '17 at 19:05
-
`tkinter` has `grid()` layout which you can use with `Label`s or `Button`s to create calendar view. – furas Dec 23 '17 at 19:18
-
btw: Python has module [calendar](https://docs.python.org/3.6/library/calendar.html) to genearet calendar in text or HTML but it can also be used to build other generator - `import calendar ; text = calendar.TextCalendar() ; print(text.formatmonth(2017, 12)) ; print(text.formatyear(2017))` – furas Dec 23 '17 at 19:26
1 Answers
4
EDIT: I found on SO: How do I create a date picker in tkinter?.
There are widgets for tkinter
which use calendar
too
(but if you just need to display events as list then you can need only Label
/Button
with pack()
or Listbox
. And this you can find in any tutorial.)
Python
has module calendar which can generate calendar as text or HTML
import calendar
text = calendar.TextCalendar()
print(text.formatmonth(2017, 12))
result:
December 2017
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Or for full year
print(text.formatyear(2017))
result:
2017
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 27 28 29 30 31
30 31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 2 3 4 5 6 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 2 3 4 5 6 1 2 3
3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10
10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17
17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24
24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 30
31
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 1 2 3
2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31
30 31
It can also gives datetime objects for selected month
print(text.monthdatescalendar(2017, 12))
Result
[[datetime.date(2017, 11, 27), datetime.date(2017, 11, 28), datetime.date(2017, 11, 29), datetime.date(2017, 11, 30), datetime.date(2017, 12, 1), datetime.date(2017, 12, 2), datetime.date(2017, 12, 3)],
[datetime.date(2017, 12, 4), datetime.date(2017, 12, 5), datetime.date(2017, 12, 6), datetime.date(2017, 12, 7), datetime.date(2017, 12, 8), datetime.date(2017, 12, 9), datetime.date(2017, 12, 10)],
[datetime.date(2017, 12, 11), datetime.date(2017, 12, 12), datetime.date(2017, 12, 13), datetime.date(2017, 12, 14), datetime.date(2017, 12, 15), datetime.date(2017, 12, 16), datetime.date(2017, 12, 17)],
[datetime.date(2017, 12, 18), datetime.date(2017, 12, 19), datetime.date(2017, 12, 20), datetime.date(2017, 12, 21), datetime.date(2017, 12, 22), datetime.date(2017, 12, 23), datetime.date(2017, 12, 24)],
[datetime.date(2017, 12, 25), datetime.date(2017, 12, 26), datetime.date(2017, 12, 27), datetime.date(2017, 12, 28), datetime.date(2017, 12, 29), datetime.date(2017, 12, 30), datetime.date(2017, 12, 31)]]
And you can use Calendar
to create class which generates tkinter.Frame
like this:
# --- class ---
import calendar
import tkinter
class TkinterCalendar(calendar.Calendar):
def formatmonth(self, master, year, month):
dates = self.monthdatescalendar(year, month)
frame = tkinter.Frame(master)
self.labels = []
for r, week in enumerate(dates):
labels_row = []
for c, date in enumerate(week):
label = tkinter.Button(frame, text=date.strftime('%Y\n%m\n%d'))
label.grid(row=r, column=c)
if date.month != month:
label['bg'] = '#aaa'
if c == 6:
label['fg'] = 'red'
labels_row.append(label)
self.labels.append(labels_row)
return frame
# --- example how to use ---
import tkinter as tk
root = tk.Tk()
tkcalendar = TkinterCalendar()
for year, month in [(2017, 11), (2017, 12), (2018,1)]:
tk.Label(root, text = '{} / {}'.format(year, month)).pack()
frame = tkcalendar.formatmonth(root, year, month)
frame.pack()
root.mainloop()
It still need to display events in calendar and assign function to buttons (every date is tkinter.Button
).

furas
- 134,197
- 12
- 106
- 148