[Python-checkins] r64025 - in sandbox/trunk/ttk-gsoc: samples/ttkcalendar.py src/2.x/ttk.py src/3.x/ttk.py

guilherme.polo python-checkins at python.org
Sat Jun 7 21:27:29 CEST 2008


Author: guilherme.polo
Date: Sat Jun  7 21:27:29 2008
New Revision: 64025

Log:
Empty values may be used at columns in ttk Treeview, now they are correctly
accepted by by _format_optdict;

Added a new sample, a calendar;

Bumped to version 0.1.2


Added:
   sandbox/trunk/ttk-gsoc/samples/ttkcalendar.py
Modified:
   sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
   sandbox/trunk/ttk-gsoc/src/3.x/ttk.py

Added: sandbox/trunk/ttk-gsoc/samples/ttkcalendar.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/ttk-gsoc/samples/ttkcalendar.py	Sat Jun  7 21:27:29 2008
@@ -0,0 +1,111 @@
+"""Simple calendar using ttk Treeview together with calendar and datetime
+classes.
+
+wrriten by Guilherme Polo, 2008.
+"""
+import calendar
+
+try:
+    import Tkinter
+    import tkFont
+except ImportError: # py3k
+    import tkinter as Tkinter
+    import tkinter.font as tkFont
+
+import ttk
+
+class Calendar(ttk.Frame):
+    datetime = calendar.datetime.datetime
+    timedelta = calendar.datetime.timedelta
+
+    def __init__(self, master=None, **kw):
+        """
+        WIDGET-SPECIFIC OPTIONS
+
+            locale, firstweekday, year, month
+        """
+        # remove custom options from kw before initializating ttk.Frame
+        self._fwday = kw.pop('firstweekday', 0)
+        year = kw.pop('year', self.datetime.now().year)
+        month = kw.pop('month', self.datetime.now().month)
+        self._date = self.datetime(year, month, 1)
+        locale = kw.pop('locale', None)
+
+        ttk.Frame.__init__(self, master, **kw)
+
+        if locale is None:
+            self._cal = calendar.TextCalendar(self._fwday)
+        else:
+            self._cal = calendar.LocaleTextCalendar(self._fwday, locale)
+
+        # custom ttk Buttons
+        style = ttk.Style(master)
+        style.layout('L.TButton', [('Button.leftarrow', None)])
+        style.layout('R.TButton', [('Button.rightarrow', None)])
+
+        # header frame and its widgets
+        hframe = ttk.Frame(self)
+        lbtn = ttk.Button(hframe, style='L.TButton', command=self._prev_month)
+        rbtn = ttk.Button(hframe, style='R.TButton', command=self._next_month)
+        self._header = ttk.Label(hframe, width=15, anchor='center')
+
+        # the calendar
+        cols = self._cal.formatweekheader(3).split()
+        self._calendar = ttk.Treeview(show='', columns=cols, selectmode='none',
+            height=7)
+        self._calendar.tag_configure('header', background='grey90')
+        self._calendar.insert('', 'end', values=cols, tag='header')
+        # adjust columns width
+        font = tkFont.Font()
+        maxwidth = max(font.measure(col) for col in cols)
+        for col in cols:
+            self._calendar.column(col, width=maxwidth, anchor='e')
+        # store treeview's tags
+        self._items = [self._calendar.insert('', 'end', values='')
+                            for _ in range(6)]
+
+        # pack the widgets
+        hframe.pack(in_=self, side='top', pady=4, anchor='center')
+        lbtn.grid(in_=hframe)
+        self._header.grid(in_=hframe, column=1, row=0, padx=12)
+        rbtn.grid(in_=hframe, column=2, row=0)
+        self._calendar.pack(in_=self, expand=1, fill='both', side='bottom')
+
+        # finally build the calendar display
+        self.__build_calendar()
+
+    def __build_calendar(self):
+        year, month = self._date.year, self._date.month
+
+        header = self._cal.formatmonthname(year, month, 0)
+        self._header['text'] = header.title()
+        #cal = self._cal.monthdatescalendar(year, month)
+        cal = self._cal.monthdayscalendar(year, month)
+
+        for indx, item in enumerate(self._items):
+            week = cal[indx] if indx < len(cal) else []
+            #y = ['%02d' % dtime.day for dtime in week]
+            y = [('%02d' % day) if day else '' for day in week]
+            self._calendar.item(self._items[indx], values=y)
+
+    def _prev_month(self):
+        self._date = self._date - self.timedelta(days=1)
+        self._date = self.datetime(self._date.year, self._date.month, 1)
+        self.__build_calendar()
+
+    def _next_month(self):
+        year, month = self._date.year, self._date.month
+        self._date = self._date + self.timedelta(
+            days=calendar.monthrange(year, month)[1] + 1)
+        self._date = self.datetime(self._date.year, self._date.month, 1)
+        self.__build_calendar()
+
+
+def test():
+    root = Tkinter.Tk()
+    x = Calendar(firstweekday=6)#, locale=('pt_BR', 'UTF-8'))
+    x.pack(expand=1, fill='both')
+    root.mainloop()
+
+if __name__ == '__main__':
+    test()

Modified: sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	Sat Jun  7 21:27:29 2008
@@ -12,7 +12,7 @@
 of the widgets appearance lies at Themes.
 """
 
-__version__ = "0.1.1"
+__version__ = "0.1.2"
 
 __author__ = "Guilherme Polo <ggpolo at gmail.com>"
 
@@ -65,7 +65,8 @@
             continue
 
         if isinstance(value, (list, tuple)):
-            value = format % ' '.join(map(unicode, value))
+            value = (unicode(val) if val else '{}' for val in value)
+            value = format % ' '.join(value)
 
         if script and value == '':
             value = '{}' # empty string in Python is equivalent to {} in Tcl

Modified: sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	Sat Jun  7 21:27:29 2008
@@ -12,7 +12,7 @@
 of the widgets appearance lies at Themes.
 """
 
-__version__ = "0.1.1"
+__version__ = "0.1.2"
 
 __author__ = "Guilherme Polo <ggpolo at gmail.com>"
 
@@ -65,7 +65,8 @@
             continue
 
         if isinstance(value, (list, tuple)):
-            value = format % ' '.join(map(str, value))
+            value = (str(val) if val else '{}' for val in value)
+            value = format % ' '.join(value)
 
         if script and value == '':
             value = '{}' # empty string in Python is equivalent to {} in Tcl


More information about the Python-checkins mailing list