[Python-checkins] r65549 - sandbox/trunk/ttk-gsoc/samples/ttkcalendar.py

guilherme.polo python-checkins at python.org
Tue Aug 5 16:52:05 CEST 2008


Author: guilherme.polo
Date: Tue Aug  5 16:52:04 2008
New Revision: 65549

Log:
Fixed representation when days < 10 were selected;
Added __getitem__ and __setitem__.


Modified:
   sandbox/trunk/ttk-gsoc/samples/ttkcalendar.py

Modified: sandbox/trunk/ttk-gsoc/samples/ttkcalendar.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/samples/ttkcalendar.py	(original)
+++ sandbox/trunk/ttk-gsoc/samples/ttkcalendar.py	Tue Aug  5 16:52:04 2008
@@ -15,7 +15,7 @@
 import ttk
 
 class Calendar(ttk.Frame):
-    # XXX ToDo: critical: __getitem__, cget and configure
+    # XXX ToDo: critical: cget and configure
 
     datetime = calendar.datetime.datetime
     timedelta = calendar.datetime.timedelta
@@ -61,6 +61,27 @@
         # set the minimal size for the widget
         self._calendar.bind('<Map>', self.__minsize)
 
+    def __setitem__(self, item, value):
+        if item in ('year', 'month'):
+            raise AttributeError("attribute '%s' is not writeable" % item)
+        elif item == 'selectbackground':
+            self._canvas['background'] = value
+        elif item == 'selectforeground':
+            self._canvas.itemconfigure(self._canvas.text, item=value)
+        else:
+            ttk.Frame.__setitem__(self, item, value)
+
+    def __getitem__(self, item):
+        if item in ('year', 'month'):
+            return getattr(self._date, item)
+        elif item == 'selectbackground':
+            return self._canvas['background']
+        elif item == 'selectforeground':
+            return self._canvas.itemcget(self._canvas.text, 'fill')
+        else:
+            r = ttk.tclobjs_to_py({item: ttk.Frame.__getitem__(self, item)})
+            return r[item]
+
     def __setup_styles(self):
         # custom ttk styles
         style = ttk.Style(self.master)
@@ -164,6 +185,7 @@
             return
 
         # update and then show selection
+        text = '%02d' % text
         self._selection = (text, item, column)
         self._show_selection(text, bbox)
 
@@ -196,8 +218,8 @@
         year, month = self._date.year, self._date.month
         return self.datetime(year, month, int(self._selection[0]))
 
-import sys
 def test():
+    import sys
     root = Tkinter.Tk()
     root.title('Ttk Calendar')
     ttkcal = Calendar(firstweekday=calendar.SUNDAY)


More information about the Python-checkins mailing list