1"""This demonstrates good part of the syntax accepted by theme_create.
2
3This is a translation of plastik.tcl to python.
4You will need the images used by the plastik theme to test this. The
5images (and other tile themes) can be retrived by doing:
6
7$ cvs -z3 -d:pserver:anonymous@tktable.cvs.sourceforge.net:/cvsroot/tktable \
8  co tile-themes
9
10To test this module you should do, for example:
11
12import Tkinter
13import plastik_theme
14
15root = Tkinter.Tk()
16plastik_theme.install(plastik_image_dir)
17...
18
19Where plastik_image_dir contains the path to the images directory used by
20the plastik theme, something like: tile-themes/plastik/plastik
21"""
22import os
23import glob
24import ttk
25from Tkinter import PhotoImage
26
27__all__ = ['install']
28
29colors = {
30    "frame": "#efefef",
31    "disabledfg": "#aaaaaa",
32    "selectbg": "#657a9e",
33    "selectfg": "#ffffff"
34    }
35
36imgs = {}
37def _load_imgs(imgdir):
38    imgdir = os.path.expanduser(imgdir)
39    if not os.path.isdir(imgdir):
40        raise Exception("%r is not a directory, can't load images" % imgdir)
41    for f in glob.glob("%s/*.gif" % imgdir):
42        img = os.path.split(f)[1]
43        name = img[:-4]
44        imgs[name] = PhotoImage(name, file=f, format="gif89")
45
46def install(imgdir):
47    _load_imgs(imgdir)
48    style = ttk.Style()
49    style.theme_create("plastik", "default", settings={
50        ".": {
51            "configure":
52                {"background": colors['frame'],
53                 "troughcolor": colors['frame'],
54                 "selectbackground": colors['selectbg'],
55                 "selectforeground": colors['selectfg'],
56                 "fieldbackground": colors['frame'],
57                 "font": "TkDefaultFont",
58                 "borderwidth": 1},
59            "map": {"foreground": [("disabled", colors['disabledfg'])]}
60        },
61
62        "Vertical.TScrollbar": {"layout": [
63            ("Vertical.Scrollbar.uparrow", {"side": "top", "sticky": ''}),
64            ("Vertical.Scrollbar.downarrow", {"side": "bottom", "sticky": ''}),
65            ("Vertical.Scrollbar.uparrow", {"side": "bottom", "sticky": ''}),
66            ("Vertical.Scrollbar.trough", {"sticky": "ns", "children":
67                [("Vertical.Scrollbar.thumb", {"expand": 1, "unit": 1,
68                    "children": [("Vertical.Scrollbar.grip", {"sticky": ''})]
69                })]
70            })]
71        },
72
73        "Horizontal.TScrollbar": {"layout": [
74            ("Horizontal.Scrollbar.leftarrow", {"side": "left", "sticky": ''}),
75            ("Horizontal.Scrollbar.rightarrow",
76                {"side": "right", "sticky": ''}),
77            ("Horizontal.Scrollbar.leftarrow",
78                {"side": "right", "sticky": ''}),
79            ("Horizontal.Scrollbar.trough", {"sticky": "ew", "children":
80                [("Horizontal.Scrollbar.thumb", {"expand": 1, "unit": 1,
81                    "children": [("Horizontal.Scrollbar.grip", {"sticky": ''})]
82                })]
83            })]
84        },
85
86        "TButton": {
87            "configure": {"width": 10, "anchor": "center"},
88            "layout": [
89                ("Button.button", {"children":
90                    [("Button.focus", {"children":
91                        [("Button.padding", {"children":
92                            [("Button.label", {"side": "left", "expand": 1})]
93                        })]
94                    })]
95                })
96            ]
97        },
98
99        "Toolbutton": {
100            "configure": {"anchor": "center"},
101            "layout": [
102                ("Toolbutton.border", {"children":
103                    [("Toolbutton.button", {"children":
104                        [("Toolbutton.padding", {"children":
105                            [("Toolbutton.label", {"side":"left", "expand":1})]
106                        })]
107                    })]
108                })
109            ]
110        },
111
112        "TMenubutton": {"layout": [
113            ("Menubutton.button", {"children":
114                [("Menubutton.indicator", {"side": "right"}),
115                 ("Menubutton.focus", {"children":
116                    [("Menubutton.padding", {"children":
117                        [("Menubutton.label", {"side": "left", "expand": 1})]
118                    })]
119                })]
120            })]
121        },
122
123        "TNotebook": {"configure": {"tabmargins": [0, 2, 0, 0]}},
124        "TNotebook.tab": {
125            "configure": {"padding": [6, 2, 6, 2], "expand": [0, 0, 2]},
126            "map": {"expand": [("selected", [1, 2, 4, 2])]}
127        },
128        "Treeview": {"configure": {"padding": 0}},
129
130        # elements
131        "Button.button": {"element create":
132            ("image", 'button-n',
133                ("pressed", 'button-p'), ("active", 'button-h'),
134                {"border": [4, 10], "padding": 4, "sticky":"ewns"}
135            )
136        },
137
138        "Toolbutton.button": {"element create":
139            ("image", 'tbutton-n',
140                ("selected", 'tbutton-p'), ("pressed", 'tbutton-p'),
141                ("active", 'tbutton-h'),
142                {"border": [4, 9], "padding": 3, "sticky": "news"}
143            )
144        },
145
146        "Checkbutton.indicator": {"element create":
147            ("image", 'check-nu',
148                ('active', 'selected', 'check-hc'),
149                ('pressed', 'selected', 'check-pc'),
150                ('active', 'check-hu'),
151                ("selected", 'check-nc'),
152                {"sticky": ''}
153            )
154        },
155
156        "Radiobutton.indicator": {"element create":
157            ("image", 'radio-nu',
158                ('active', 'selected', 'radio-hc'),
159                ('pressed', 'selected', 'radio-pc'),
160                ('active', 'radio-hu'), ('selected', 'radio-nc'),
161                {"sticky": ''}
162            )
163        },
164
165        "Horizontal.Scrollbar.thumb": {"element create":
166            ("image", 'hsb-n', {"border": 3, "sticky": "ew"})
167        },
168
169        "Horizontal.Scrollbar.grip": {"element create": ("image", 'hsb-g')},
170        "Horizontal.Scrollbar.trough": {"element create": ("image", 'hsb-t')},
171        "Vertical.Scrollbar.thumb": {"element create":
172            ("image", 'vsb-n', {"border": 3, "sticky": "ns"})
173        },
174        "Vertical.Scrollbar.grip": {"element create": ("image", 'vsb-g')},
175        "Vertical.Scrollbar.trough": {"element create": ("image", 'vsb-t')},
176        "Scrollbar.uparrow": {"element create":
177            ("image", 'arrowup-n', ("pressed", 'arrowup-p'), {"sticky": ''})
178        },
179        "Scrollbar.downarrow": {"element create":
180            ("image", 'arrowdown-n',
181            ("pressed", 'arrowdown-p'), {'sticky': ''})
182        },
183        "Scrollbar.leftarrow": {"element create":
184            ("image", 'arrowleft-n',
185            ("pressed", 'arrowleft-p'), {'sticky': ''})
186        },
187        "Scrollbar.rightarrow": {"element create":
188            ("image", 'arrowright-n', ("pressed", 'arrowright-p'),
189            {'sticky': ''})
190        },
191
192        "Horizontal.Scale.slider": {"element create":
193            ("image", 'hslider-n', {'sticky': ''})
194        },
195        "Horizontal.Scale.trough": {"element create":
196            ("image", 'hslider-t', {'border': 1, 'padding': 0})
197        },
198        "Vertical.Scale.slider": {"element create":
199            ("image", 'vslider-n', {'sticky': ''})
200        },
201        "Vertical.Scale.trough": {"element create":
202            ("image", 'vslider-t', {'border': 1, 'padding': 0})
203        },
204
205        "Entry.field": {"element create":
206            ("image", 'entry-n',
207                ("focus", 'entry-f'),
208                {'border': 2, 'padding': [3, 4], 'sticky': 'news'}
209            )
210        },
211
212        "Labelframe.border": {"element create":
213            ("image", 'border', {'border': 4, 'padding': 4, 'sticky': 'news'})
214        },
215
216        "Menubutton.button": {"element create":
217            ("image", 'combo-r',
218                ('active', 'combo-ra'),
219                {'sticky': 'news', 'border': [4, 6, 24, 15],
220                 'padding': [4, 4, 5]}
221            )
222        },
223        "Menubutton.indicator": {"element create":
224            ("image", 'arrow-d', {"sticky": "e", "border": [15, 0, 0, 0]})
225        },
226
227        "Combobox.field": {"element create":
228            ("image", 'combo-n',
229                ('readonly', 'active', 'combo-ra'),
230                ('focus', 'active', 'combo-fa'),
231                ('active', 'combo-a'), ('!readonly', 'focus', 'combo-f'),
232                ('readonly', 'combo-r'),
233                {'border': [4, 6, 24, 15], 'padding': [4, 4, 5],
234                 'sticky': 'news'}
235            )
236        },
237        "Combobox.downarrow": {"element create":
238            ("image", 'arrow-d', {'sticky': 'e', 'border': [15, 0, 0, 0]})
239         },
240
241        "Notebook.client": {"element create":
242            ("image", 'notebook-c', {'border': 4})
243        },
244        "Notebook.tab": {"element create":
245            ("image", 'notebook-tn',
246                ("selected", 'notebook-ts'), ("active", 'notebook-ta'),
247                {'padding': [0, 2, 0, 0], 'border': [4, 10, 4, 10]}
248            )
249        },
250
251        "Progressbar.trough": {"element create":
252            ("image", 'hprogress-t', {'border': 2})
253        },
254        "Horizontal.Progressbar.pbar": {"element create":
255            ("image", 'hprogress-b', {'border': [2, 9]})
256        },
257        "Vertical.Progressbar.pbar": {"element create":
258            ("image", 'vprogress-b', {'border': [9, 2]})
259        },
260
261        "Treeheading.cell": {"element create":
262            ("image", 'tree-n',
263                ("pressed", 'tree-p'),
264                {'border': [4, 10], 'padding': 4, 'sticky': 'news'}
265            )
266        }
267
268    })
269    style.theme_use("plastik")
270