TkCalendar
Verfasst: Sonntag 18. Juli 2004, 14:53
Hi!
Vor einiger Zeit hat uns Melanie17 ein großartiges (
) Stück Code zum Anzeigen eines Kalenders gepostet. Irgendwie fehlt so etwas noch für Tkinter.
Hab mal das folgende zusammengeschustert:
Würde mich sehr über Kritik freuen.
PS: Die erste Version war noch ohne calendar.
Gruß, mawe
Vor einiger Zeit hat uns Melanie17 ein großartiges (
Hab mal das folgende zusammengeschustert:
Code: Alles auswählen
#!/usr/bin/env python
'''
Description: a calendar-widget for Tkinter
Version: 0.2
Copyright: 2004 by Markus Weihs (ehm_dot_weihs_at_utanet_dot_at)
Created: 2004-07-17
Last modified: 2004-07-18
Licence: free
Requirements: Python,Tkinter
'''
import Tkinter as tk
from calendar import setfirstweekday, monthcalendar
class TkCalendar(tk.Frame):
def __init__(self,y,m,us=0):
tk.Frame.__init__(self,bd=2,relief=tk.GROOVE)
self.year = y
self.month = m
self.date = tk.StringVar()
if us:
self.dayArray = ["Su","Mo","Tu","We","Th","Fr","Sa"]
self.col = 0
setfirstweekday(6)
else:
self.dayArray = ["Mo","Di","Mi","Do","Fr","Sa","So"]
self.col = 6
f = tk.Frame(self)
f.pack(fill=tk.X)
tk.Button(f,
text='<',
command=self.__decrease,
relief=tk.FLAT).pack(side=tk.LEFT)
self.l = tk.Label(f,text="%.2i.%i" % (self.month,self.year))
self.l.pack(side=tk.LEFT,padx=12)
tk.Button(f,
text='>',
command=self.__increase,
relief=tk.FLAT).pack(side=tk.RIGHT)
self.c = tk.Canvas(self,
width=140,height=135,
bg='white',bd=2,relief=tk.GROOVE)
self.c.bind('<1>', self.__click)
self.c.pack()
self.__fill_canvas()
def __fill_canvas(self):
m = monthcalendar(self.year,self.month)
for col in range(len(m[0])):
for row in range(len(m)):
if m[row][col]==0:
pass
else:
if col==self.col:
self.c.create_text(
col*20+12,row*20+30,
text="%2i" % m[row][col],fill='red',tags='day')
else:
self.c.create_text(
col*20+12,row*20+30,
text="%2i" % m[row][col],tags='day')
x=12; y=10
for i in self.dayArray:
self.c.create_text(x,y,text=i,fill='blue', tags='day')
x+=20
def __decrease(self):
self.c.delete('day')
if self.month == 1:
self.year -= 1
self.month = 12
else:
self.month -= 1
self.l.configure(text="%.2i.%i" % (self.month, self.year))
self.__fill_canvas()
def __increase(self):
self.c.delete('day')
if self.month == 12:
self.year += 1
self.month = 1
else:
self.month += 1
self.l.configure(text="%.2i.%i" % (self.month, self.year))
self.__fill_canvas()
def __click(self,event):
x = self.c.find_closest(event.x,event.y)[0]
try:
day = self.c.itemcget(x,'text')
self.date.set("%.2i.%.2i.%i" % (int(day),self.month,self.year))
print self.date.get()
except: pass
if __name__ == '__main__':
from time import localtime
year,month = localtime()[0:2]
root = tk.Tk()
c = TkCalendar(year, month) # european version
#c = TkCalendar(year, month, us=1) # us version
c.pack()
root.mainloop()
PS: Die erste Version war noch ohne calendar.
Gruß, mawe