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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| from tkinter import *
import calendar
import tkinter as tk
import datetime
import locale
class Data:
def __init__(self):
self.day_selected = 0
self.month_selected = 0
self.year_selected = 0
self.day_name = 0
class Calendar:
def __init__(self, parent, data):
self.data = data
self.parent = parent
self.cal = calendar.TextCalendar(0)
D= datetime.date.today()
self.year = D.year
self.month = D.month
self.wid = []
self.day_selected = D.day
self.month_selected = self.month
self.year_selected = self.year
self.day_name = ''
self.setup(self.year, self.month)
def clear(self):
for w in self.wid[:]:
w.grid_forget()
self.wid.remove(w)
def go_prev(self):
if self.month > 1:
self.month -= 1
else:
self.month = 12
self.year -= 1
self.clear()
self.setup(self.year, self.month)
def go_next(self):
if self.month < 12:
self.month += 1
else:
self.month = 1
self.year += 1
self.clear()
self.setup(self.year, self.month)
def selection(self, day, name):
self.day_selected = day
self.month_selected = self.month
self.year_selected = self.year
self.day_name = name
self.data.day_selected = day
self.data.month_selected = self.month
self.data.year_selected = self.year
self.data.day_name = name
self.clear()
self.setup(self.year, self.month)
def setup(self, y, m):
Mmoins = Button(self.parent, text='<', command=self.go_prev)
self.wid.append(Mmoins)
Mmoins.grid(row=0, column=1)
header = Label(self.parent, height=2, text='{} {}'.format(calendar.month_abbr[m], str(y)))
self.wid.append(header)
header.grid(row=0, column=2, columnspan=3)
Mplus = Button(self.parent, text='>', command=self.go_next)
self.wid.append(Mplus)
Mplus.grid(row=0, column=5)
days = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche']
for num, jj in enumerate(days):
t = Label(self.parent, text=jj[:3])
self.wid.append(t)
t.grid(row=1, column=num)
for w, week in enumerate(self.cal.monthdayscalendar(y, m), 2):
for d, day in enumerate(week):
if day:
b = Button(self.parent, width=1, text=day, command=lambda day=day:self.selection(day, calendar.day_name[(day) % 7]))
self.wid.append(b)
b.grid(row=w, column=d)
sel = Label(self.parent, height=2, text='{} {} {} {}'.format(self.day_name, self.day_selected, calendar.month_name[self.month_selected], self.year_selected))
self.wid.append(sel)
sel.grid(row=8, column=0, columnspan=7)
ok = Button(self.parent, width=5, text='OK', command='disabled')
self.wid.append(ok)
ok.grid(row=9, column=2, columnspan=3, pady=10)
def Wcal(parent, d):
Wcal = Toplevel(parent)
locale.setlocale(locale.LC_ALL, '')
cal = Calendar(Wcal, d)
data = Data()
FP = Tk()
Button(FP, text='calendar', command=lambda:Wcal(FP, data)).grid()
FP.mainloop() |
Partager