Skip to content

Instantly share code, notes, and snippets.

@m13253
Created June 10, 2016 15:21
Show Gist options
  • Save m13253/e495ae7a7045cde666395c32ca6eed93 to your computer and use it in GitHub Desktop.
Save m13253/e495ae7a7045cde666395c32ca6eed93 to your computer and use it in GitHub Desktop.
A simple Tkinter calendar with memo feature
#!/usr/bin/env python2
import calendar
import datetime
import os.path
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.master = master
today = datetime.date.today()
self.year, self.month = today.year, today.month
btn_prev = Button(self, text='<', command=self.do_prev)
btn_prev.grid(row=0, column=0, sticky='nesw')
btn_next = Button(self, text='>', command=self.do_next)
btn_next.grid(row=0, column=6, sticky='nesw')
for day_idx, day_name in [
(0, 'Sun'), (1, 'Mon'), (2, 'Tue'), (3, 'Wed'), (4, 'Thu'), (5, 'Fri'), (6, 'Sat')
]:
lbl_weekday = Label(self, text=day_name)
lbl_weekday.grid(row=1, column=day_idx, sticky='nesw')
self.elements = []
self.draw_calendar()
self.pack(expand=True)
self.memo = Memo(self)
def draw_calendar(self):
for element in self.elements:
element.grid_forget()
self.elements = []
lbl_date = Label(self, text='%s %d' % ([
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
][self.month-1], self.year))
lbl_date.grid(row=0, column=1, columnspan=5, sticky='nesw')
self.elements.append(lbl_date)
cal = calendar.Calendar()
for week_idx, week in enumerate(cal.monthdatescalendar(self.year, self.month)):
for day_idx, day in enumerate(week):
if day.month == self.month:
el_date = Button(self, text=day.day, command=lambda date=day: self.memo.show(date))
else:
el_date = Label(self, text=day.day)
if os.path.isfile('memo-%s.txt' % day):
el_date['text'] = '[%d]' % day.day
el_date.grid(row=week_idx+2, column=day_idx, sticky='nesw')
self.elements.append(el_date)
def do_prev(self):
self.month -= 1
if self.month == 0:
self.year -= 1
self.month = 12
self.draw_calendar()
def do_next(self):
self.month += 1
if self.month == 13:
self.year += 1
self.month = 1
self.draw_calendar()
class Memo(Frame):
def __init__(self, app):
Frame.__init__(self, app.master)
self.app = app
self.txt_edit = Text(self)
self.txt_edit.pack(side=TOP)
self.btn_ok = Button(self, text='OK', command=self.submit)
self.btn_ok.pack(side=BOTTOM)
def show(self, date):
self.date = date
self.txt_edit.delete('1.0', 'end')
try:
f = open('memo-%s.txt' % date, 'rb')
text = f.read().decode('utf-8', 'replace')
f.close()
except Exception:
text = ''
self.txt_edit.insert('1.0', text)
app.pack_forget()
self.pack(expand=True)
self.txt_edit.focus()
def submit(self):
self.pack_forget()
self.app.pack(expand=True)
text = self.txt_edit.get('1.0', 'end').strip()
try:
if text != '':
f = open('memo-%s.txt' % self.date, 'wb')
f.write(text.encode('utf-8', 'replace'))
f.close()
else:
os.remove('memo-%s.txt' % self.date)
except Exception:
pass
self.app.draw_calendar()
if __name__ == '__main__':
root = Tk()
app = Application(root)
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment