Default repository for tklayout.py.
Revisão | 4404375b60a36308c7cb6ff1261c9f32779ab8b2 (tree) |
---|---|
Hora | 2018-02-26 13:19:48 |
Autor | Dreas Nielsen <dreas.nielsen@gmai...> |
Commiter | Dreas Nielsen |
Changes to prepare to allow panes to appear on separate notebook panes instead of just arranged vertically or horizontally.
@@ -2,7 +2,7 @@ | ||
2 | 2 | |
3 | 3 | setup(name='tklayout', |
4 | 4 | packages=['tklayout'], |
5 | - version='0.8.0', | |
5 | + version='0.9.0', | |
6 | 6 | description="Simplifies the implementation of Tkinter UI layouts by allowing the developer to describe the hierarchy of UI elements from the inside out.", |
7 | 7 | author='Dreas Nielsen', |
8 | 8 | author_email='dreas.nielsen@gmail.com', |
@@ -27,7 +27,7 @@ | ||
27 | 27 | other widgets to create a Tkinter UI. |
28 | 28 | """ |
29 | 29 | |
30 | -__version__ = '0.8.0' | |
30 | +__version__ = '0.9.0' | |
31 | 31 | |
32 | 32 | try: |
33 | 33 | import Tkinter as tk |
@@ -56,7 +56,6 @@ | ||
56 | 56 | """Represents the structure of Tkinter widgets (e.g., frames) and provides |
57 | 57 | methods to create and populate the nested set of frames. |
58 | 58 | """ |
59 | - | |
60 | 59 | class Element(object): |
61 | 60 | """An Element object describes one Tkinter element--a Frame or other widget. |
62 | 61 |
@@ -64,7 +63,7 @@ | ||
64 | 63 | * A list of component names (names are strings). |
65 | 64 | * A dictionary of configuration values for the frame that will contain the components. |
66 | 65 | * A dictionary of gridding options for the frame that will contain the components. |
67 | - * A flag indicating whether the component elements are arranged horizontally or vertically. | |
66 | + * A string indicating whether the component elements are arranged horizontally, vertically, or on parallel pages. | |
68 | 67 | * A weight or list of weights for the rows or columns represented |
69 | 68 | by the components. If this list is shorter than the list of |
70 | 69 | components, its members will be recycled as many times as necessary |
@@ -72,12 +71,16 @@ | ||
72 | 71 | * The weight for the column or row in which all of the elements appear. |
73 | 72 | """ |
74 | 73 | |
75 | - def __init__(self, components, config_dict=None, grid_opts=None, vertical=True, component_weights=[1], element_weight=1): | |
74 | + arrangements = {"vertical", "horizontal", "paged"} | |
75 | + | |
76 | + def __init__(self, components, config_dict=None, grid_opts=None, arrangement="vertical", component_weights=[1], element_weight=1): | |
76 | 77 | """Assign all data values to the new Element object.""" |
78 | + if arrangement not in self.arrangements: | |
79 | + raise AppLayoutError("Unrecognized arrangement: %s." % arrangement) | |
77 | 80 | self.components = components |
78 | 81 | self.config_dict = config_dict |
79 | 82 | self.grid_opts = grid_opts |
80 | - self.vertical = vertical | |
83 | + self.arrangement = arrangement | |
81 | 84 | self.component_weights = component_weights |
82 | 85 | self.element_weight = element_weight |
83 | 86 | self.frame = None # Will be assigned by create_layout() |
@@ -117,10 +120,11 @@ | ||
117 | 120 | the named elements. |
118 | 121 | :return: A name (string) that is automatically assigned to the element that is created |
119 | 122 | to contain the named elements. This name may be used in the *element_names* |
120 | - argument to subsequent calls to *column_elements()* or *row_elements()*. | |
123 | + argument to subsequent calls to *column_elements()*, *row_elements()*. | |
124 | + or *page_elements*. | |
121 | 125 | """ |
122 | 126 | wts = replist(row_weights, len(element_names)) |
123 | - el = self.Element(element_names, config_dict, grid_dict, True, component_weights=wts, element_weight=column_weight) | |
127 | + el = self.Element(element_names, config_dict, grid_dict, "vertical", component_weights=wts, element_weight=column_weight) | |
124 | 128 | el_id = uuid.uuid4() |
125 | 129 | self.partslist[el_id] = el |
126 | 130 | self.synthnames.append(el_id) |
@@ -129,8 +133,9 @@ | ||
129 | 133 | el_n = self.Element(None) |
130 | 134 | self.partslist[n] = el_n |
131 | 135 | return el_id |
136 | + | |
132 | 137 | def row_elements(self, element_names, config_dict=None, grid_dict=None, column_weights=[1], row_weight=1): |
133 | - """Takes a list of names of elements to be vertically arranged, creates a new element | |
138 | + """Takes a list of names of elements to be horizontally arranged, creates a new element | |
134 | 139 | that encloses those, and returns a synthesized name for the element that is created. |
135 | 140 | |
136 | 141 | :param element_names: A list of names (strings) of elements. |
@@ -145,10 +150,11 @@ | ||
145 | 150 | the named elements. |
146 | 151 | :return: A name (string) that is automatically assigned to the element that is created |
147 | 152 | to contain the named elements. This name may be used in the *element_names* |
148 | - argument to subsequent calls to *column_elements()* or *row_elements()*. | |
153 | + argument to subsequent calls to *column_elements()*, *row_elements()*. | |
154 | + or *page_elements*. | |
149 | 155 | """ |
150 | 156 | wts = replist(column_weights, len(element_names)) |
151 | - el = self.Element(element_names, config_dict, grid_dict, False, component_weights=wts, element_weight=row_weight) | |
157 | + el = self.Element(element_names, config_dict, grid_dict, "horizontal", component_weights=wts, element_weight=row_weight) | |
152 | 158 | el_id = uuid.uuid4() |
153 | 159 | self.partslist[el_id] = el |
154 | 160 | self.synthnames.append(el_id) |
@@ -157,11 +163,37 @@ | ||
157 | 163 | el_n = self.Element(None) |
158 | 164 | self.partslist[n] = el_n |
159 | 165 | return el_id |
166 | + | |
167 | + def page_elements(self, element_names, config_dict=None, grid_dict=None): | |
168 | + """Takes a list of names of elements to be arranged on separate pages or panes | |
169 | + (e.g., pages of a Notebook widget), creates a new element that contains | |
170 | + those, and returns a synthesized name for the element that is created. | |
171 | + | |
172 | + :param element_names: A list of names (strings) of elements. | |
173 | + :param config_dict: A dictionary of configuration specifications for the frame that will | |
174 | + contain the named elements. | |
175 | + :param grid_dict: A dictionary of gridding options for the frame that will contain | |
176 | + the named elements. | |
177 | + :return: A name (string) that is automatically assigned to the element that is created | |
178 | + to contain the named elements. This name may be used in the *element_names* | |
179 | + argument to subsequent calls to *column_elements()*, *row_elements()*, | |
180 | + or *page_elements*. | |
181 | + """ | |
182 | + el = self.Element(element_names, config_dict, grid_dict, "paged") | |
183 | + el_id = uuid.uuid4() | |
184 | + self.partslist[el_id] = el | |
185 | + self.synthnames.append(el_id) | |
186 | + for n in element_names: | |
187 | + if n not in self.synthnames: | |
188 | + el_n = self.Element(None) | |
189 | + self.partslist[n] = el_n | |
190 | + return el_id | |
191 | + | |
160 | 192 | def create_layout(self, master_widget, master_element_name, row=0, column=0, row_weight=1, column_weight=1): |
161 | 193 | """Creates a nested set of frames corresponding to the application layout that has been specified. |
162 | 194 | |
163 | - The application layout must have been previously described by calls to the 'column_elements()' and | |
164 | - 'row_elements()' methods. | |
195 | + The application layout must have been previously described by calls to the ``column_elements``, | |
196 | + ``row_elements``, and ``page_elements`` methods. | |
165 | 197 | |
166 | 198 | :param master_widget: A container widget (e.g., Frame) that will serve as the root for all the frames to be created. |
167 | 199 | :param master_element_name: The name of the element to be placed in the top-level frame. This name must be one of those supplied to 'row_elements()' or 'column_elements()', or returned by those methods. |
@@ -184,15 +216,15 @@ | ||
184 | 216 | for i, comp in enumerate(el.components): |
185 | 217 | layout_element(el.frame, comp) |
186 | 218 | compframe = self.partslist[comp].frame |
187 | - if el.vertical: | |
219 | + if el.arrangement == "vertical": | |
188 | 220 | compframe.grid(row=i, column=0) |
189 | 221 | el.frame.rowconfigure(i, weight=el.component_weights[i]) |
190 | - else: | |
222 | + elif el.arrangement == "horizontal": | |
191 | 223 | compframe.grid(row=0, column=i) |
192 | 224 | el.frame.columnconfigure(i, weight=el.component_weights[i]) |
193 | - if el.vertical: | |
225 | + if el.arrangement == "vertical": | |
194 | 226 | el.frame.columnconfigure(0, weight=el.element_weight) |
195 | - else: | |
227 | + elif el.arrangement == "horizontal": | |
196 | 228 | el.frame.rowconfigure(0, weight=el.element_weight) |
197 | 229 | layout_element(master_widget, master_element_name) |
198 | 230 | master_widget.rowconfigure(row, weight=row_weight) |
@@ -271,7 +303,7 @@ | ||
271 | 303 | if el.components and len(el.components) > 0: |
272 | 304 | json_str += u"," |
273 | 305 | if el.components and len(el.components) > 0: |
274 | - json_str = u'%s\n%s"orientation": "%s",\n' % (json_str, indentstr * (indent+1), u"vertical" if el.vertical else u"horizontal") | |
306 | + json_str = u'%s\n%s"orientation": "%s",\n' % (json_str, indentstr * (indent+1), el.arrangement) | |
275 | 307 | json_str = u'\n%s%s"elements": {' % (json_str, indentstr * (indent+1)) |
276 | 308 | for i, comp in enumerate(el.components): |
277 | 309 | json_str = describe_element(json_str, comp, i, len(el.components), indent+2, show_attributes) |