| 1 | n/a | """An implementation of tabbed pages using only standard Tkinter. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Originally developed for use in IDLE. Based on tabpage.py. |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | Classes exported: |
|---|
| 6 | n/a | TabbedPageSet -- A Tkinter implementation of a tabbed-page widget. |
|---|
| 7 | n/a | TabSet -- A widget containing tabs (buttons) in one or more rows. |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | """ |
|---|
| 10 | n/a | from tkinter import * |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | class InvalidNameError(Exception): pass |
|---|
| 13 | n/a | class AlreadyExistsError(Exception): pass |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | class TabSet(Frame): |
|---|
| 17 | n/a | """A widget containing tabs (buttons) in one or more rows. |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | Only one tab may be selected at a time. |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | """ |
|---|
| 22 | n/a | def __init__(self, page_set, select_command, |
|---|
| 23 | n/a | tabs=None, n_rows=1, max_tabs_per_row=5, |
|---|
| 24 | n/a | expand_tabs=False, **kw): |
|---|
| 25 | n/a | """Constructor arguments: |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | select_command -- A callable which will be called when a tab is |
|---|
| 28 | n/a | selected. It is called with the name of the selected tab as an |
|---|
| 29 | n/a | argument. |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | tabs -- A list of strings, the names of the tabs. Should be specified in |
|---|
| 32 | n/a | the desired tab order. The first tab will be the default and first |
|---|
| 33 | n/a | active tab. If tabs is None or empty, the TabSet will be initialized |
|---|
| 34 | n/a | empty. |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | n_rows -- Number of rows of tabs to be shown. If n_rows <= 0 or is |
|---|
| 37 | n/a | None, then the number of rows will be decided by TabSet. See |
|---|
| 38 | n/a | _arrange_tabs() for details. |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | max_tabs_per_row -- Used for deciding how many rows of tabs are needed, |
|---|
| 41 | n/a | when the number of rows is not constant. See _arrange_tabs() for |
|---|
| 42 | n/a | details. |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | """ |
|---|
| 45 | n/a | Frame.__init__(self, page_set, **kw) |
|---|
| 46 | n/a | self.select_command = select_command |
|---|
| 47 | n/a | self.n_rows = n_rows |
|---|
| 48 | n/a | self.max_tabs_per_row = max_tabs_per_row |
|---|
| 49 | n/a | self.expand_tabs = expand_tabs |
|---|
| 50 | n/a | self.page_set = page_set |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | self._tabs = {} |
|---|
| 53 | n/a | self._tab2row = {} |
|---|
| 54 | n/a | if tabs: |
|---|
| 55 | n/a | self._tab_names = list(tabs) |
|---|
| 56 | n/a | else: |
|---|
| 57 | n/a | self._tab_names = [] |
|---|
| 58 | n/a | self._selected_tab = None |
|---|
| 59 | n/a | self._tab_rows = [] |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | self.padding_frame = Frame(self, height=2, |
|---|
| 62 | n/a | borderwidth=0, relief=FLAT, |
|---|
| 63 | n/a | background=self.cget('background')) |
|---|
| 64 | n/a | self.padding_frame.pack(side=TOP, fill=X, expand=False) |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | self._arrange_tabs() |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def add_tab(self, tab_name): |
|---|
| 69 | n/a | """Add a new tab with the name given in tab_name.""" |
|---|
| 70 | n/a | if not tab_name: |
|---|
| 71 | n/a | raise InvalidNameError("Invalid Tab name: '%s'" % tab_name) |
|---|
| 72 | n/a | if tab_name in self._tab_names: |
|---|
| 73 | n/a | raise AlreadyExistsError("Tab named '%s' already exists" %tab_name) |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | self._tab_names.append(tab_name) |
|---|
| 76 | n/a | self._arrange_tabs() |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def remove_tab(self, tab_name): |
|---|
| 79 | n/a | """Remove the tab named <tab_name>""" |
|---|
| 80 | n/a | if not tab_name in self._tab_names: |
|---|
| 81 | n/a | raise KeyError("No such Tab: '%s" % tab_name) |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | self._tab_names.remove(tab_name) |
|---|
| 84 | n/a | self._arrange_tabs() |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | def set_selected_tab(self, tab_name): |
|---|
| 87 | n/a | """Show the tab named <tab_name> as the selected one""" |
|---|
| 88 | n/a | if tab_name == self._selected_tab: |
|---|
| 89 | n/a | return |
|---|
| 90 | n/a | if tab_name is not None and tab_name not in self._tabs: |
|---|
| 91 | n/a | raise KeyError("No such Tab: '%s" % tab_name) |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | # deselect the current selected tab |
|---|
| 94 | n/a | if self._selected_tab is not None: |
|---|
| 95 | n/a | self._tabs[self._selected_tab].set_normal() |
|---|
| 96 | n/a | self._selected_tab = None |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | if tab_name is not None: |
|---|
| 99 | n/a | # activate the tab named tab_name |
|---|
| 100 | n/a | self._selected_tab = tab_name |
|---|
| 101 | n/a | tab = self._tabs[tab_name] |
|---|
| 102 | n/a | tab.set_selected() |
|---|
| 103 | n/a | # move the tab row with the selected tab to the bottom |
|---|
| 104 | n/a | tab_row = self._tab2row[tab] |
|---|
| 105 | n/a | tab_row.pack_forget() |
|---|
| 106 | n/a | tab_row.pack(side=TOP, fill=X, expand=0) |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | def _add_tab_row(self, tab_names, expand_tabs): |
|---|
| 109 | n/a | if not tab_names: |
|---|
| 110 | n/a | return |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | tab_row = Frame(self) |
|---|
| 113 | n/a | tab_row.pack(side=TOP, fill=X, expand=0) |
|---|
| 114 | n/a | self._tab_rows.append(tab_row) |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | for tab_name in tab_names: |
|---|
| 117 | n/a | tab = TabSet.TabButton(tab_name, self.select_command, |
|---|
| 118 | n/a | tab_row, self) |
|---|
| 119 | n/a | if expand_tabs: |
|---|
| 120 | n/a | tab.pack(side=LEFT, fill=X, expand=True) |
|---|
| 121 | n/a | else: |
|---|
| 122 | n/a | tab.pack(side=LEFT) |
|---|
| 123 | n/a | self._tabs[tab_name] = tab |
|---|
| 124 | n/a | self._tab2row[tab] = tab_row |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | # tab is the last one created in the above loop |
|---|
| 127 | n/a | tab.is_last_in_row = True |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | def _reset_tab_rows(self): |
|---|
| 130 | n/a | while self._tab_rows: |
|---|
| 131 | n/a | tab_row = self._tab_rows.pop() |
|---|
| 132 | n/a | tab_row.destroy() |
|---|
| 133 | n/a | self._tab2row = {} |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | def _arrange_tabs(self): |
|---|
| 136 | n/a | """ |
|---|
| 137 | n/a | Arrange the tabs in rows, in the order in which they were added. |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | If n_rows >= 1, this will be the number of rows used. Otherwise the |
|---|
| 140 | n/a | number of rows will be calculated according to the number of tabs and |
|---|
| 141 | n/a | max_tabs_per_row. In this case, the number of rows may change when |
|---|
| 142 | n/a | adding/removing tabs. |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | """ |
|---|
| 145 | n/a | # remove all tabs and rows |
|---|
| 146 | n/a | while self._tabs: |
|---|
| 147 | n/a | self._tabs.popitem()[1].destroy() |
|---|
| 148 | n/a | self._reset_tab_rows() |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | if not self._tab_names: |
|---|
| 151 | n/a | return |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | if self.n_rows is not None and self.n_rows > 0: |
|---|
| 154 | n/a | n_rows = self.n_rows |
|---|
| 155 | n/a | else: |
|---|
| 156 | n/a | # calculate the required number of rows |
|---|
| 157 | n/a | n_rows = (len(self._tab_names) - 1) // self.max_tabs_per_row + 1 |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | # not expanding the tabs with more than one row is very ugly |
|---|
| 160 | n/a | expand_tabs = self.expand_tabs or n_rows > 1 |
|---|
| 161 | n/a | i = 0 # index in self._tab_names |
|---|
| 162 | n/a | for row_index in range(n_rows): |
|---|
| 163 | n/a | # calculate required number of tabs in this row |
|---|
| 164 | n/a | n_tabs = (len(self._tab_names) - i - 1) // (n_rows - row_index) + 1 |
|---|
| 165 | n/a | tab_names = self._tab_names[i:i + n_tabs] |
|---|
| 166 | n/a | i += n_tabs |
|---|
| 167 | n/a | self._add_tab_row(tab_names, expand_tabs) |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | # re-select selected tab so it is properly displayed |
|---|
| 170 | n/a | selected = self._selected_tab |
|---|
| 171 | n/a | self.set_selected_tab(None) |
|---|
| 172 | n/a | if selected in self._tab_names: |
|---|
| 173 | n/a | self.set_selected_tab(selected) |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | class TabButton(Frame): |
|---|
| 176 | n/a | """A simple tab-like widget.""" |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | bw = 2 # borderwidth |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | def __init__(self, name, select_command, tab_row, tab_set): |
|---|
| 181 | n/a | """Constructor arguments: |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | name -- The tab's name, which will appear in its button. |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | select_command -- The command to be called upon selection of the |
|---|
| 186 | n/a | tab. It is called with the tab's name as an argument. |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | """ |
|---|
| 189 | n/a | Frame.__init__(self, tab_row, borderwidth=self.bw, relief=RAISED) |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | self.name = name |
|---|
| 192 | n/a | self.select_command = select_command |
|---|
| 193 | n/a | self.tab_set = tab_set |
|---|
| 194 | n/a | self.is_last_in_row = False |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | self.button = Radiobutton( |
|---|
| 197 | n/a | self, text=name, command=self._select_event, |
|---|
| 198 | n/a | padx=5, pady=1, takefocus=FALSE, indicatoron=FALSE, |
|---|
| 199 | n/a | highlightthickness=0, selectcolor='', borderwidth=0) |
|---|
| 200 | n/a | self.button.pack(side=LEFT, fill=X, expand=True) |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | self._init_masks() |
|---|
| 203 | n/a | self.set_normal() |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | def _select_event(self, *args): |
|---|
| 206 | n/a | """Event handler for tab selection. |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | With TabbedPageSet, this calls TabbedPageSet.change_page, so that |
|---|
| 209 | n/a | selecting a tab changes the page. |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | Note that this does -not- call set_selected -- it will be called by |
|---|
| 212 | n/a | TabSet.set_selected_tab, which should be called when whatever the |
|---|
| 213 | n/a | tabs are related to changes. |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | """ |
|---|
| 216 | n/a | self.select_command(self.name) |
|---|
| 217 | n/a | return |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | def set_selected(self): |
|---|
| 220 | n/a | """Assume selected look""" |
|---|
| 221 | n/a | self._place_masks(selected=True) |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | def set_normal(self): |
|---|
| 224 | n/a | """Assume normal look""" |
|---|
| 225 | n/a | self._place_masks(selected=False) |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | def _init_masks(self): |
|---|
| 228 | n/a | page_set = self.tab_set.page_set |
|---|
| 229 | n/a | background = page_set.pages_frame.cget('background') |
|---|
| 230 | n/a | # mask replaces the middle of the border with the background color |
|---|
| 231 | n/a | self.mask = Frame(page_set, borderwidth=0, relief=FLAT, |
|---|
| 232 | n/a | background=background) |
|---|
| 233 | n/a | # mskl replaces the bottom-left corner of the border with a normal |
|---|
| 234 | n/a | # left border |
|---|
| 235 | n/a | self.mskl = Frame(page_set, borderwidth=0, relief=FLAT, |
|---|
| 236 | n/a | background=background) |
|---|
| 237 | n/a | self.mskl.ml = Frame(self.mskl, borderwidth=self.bw, |
|---|
| 238 | n/a | relief=RAISED) |
|---|
| 239 | n/a | self.mskl.ml.place(x=0, y=-self.bw, |
|---|
| 240 | n/a | width=2*self.bw, height=self.bw*4) |
|---|
| 241 | n/a | # mskr replaces the bottom-right corner of the border with a normal |
|---|
| 242 | n/a | # right border |
|---|
| 243 | n/a | self.mskr = Frame(page_set, borderwidth=0, relief=FLAT, |
|---|
| 244 | n/a | background=background) |
|---|
| 245 | n/a | self.mskr.mr = Frame(self.mskr, borderwidth=self.bw, |
|---|
| 246 | n/a | relief=RAISED) |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | def _place_masks(self, selected=False): |
|---|
| 249 | n/a | height = self.bw |
|---|
| 250 | n/a | if selected: |
|---|
| 251 | n/a | height += self.bw |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | self.mask.place(in_=self, |
|---|
| 254 | n/a | relx=0.0, x=0, |
|---|
| 255 | n/a | rely=1.0, y=0, |
|---|
| 256 | n/a | relwidth=1.0, width=0, |
|---|
| 257 | n/a | relheight=0.0, height=height) |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | self.mskl.place(in_=self, |
|---|
| 260 | n/a | relx=0.0, x=-self.bw, |
|---|
| 261 | n/a | rely=1.0, y=0, |
|---|
| 262 | n/a | relwidth=0.0, width=self.bw, |
|---|
| 263 | n/a | relheight=0.0, height=height) |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | page_set = self.tab_set.page_set |
|---|
| 266 | n/a | if selected and ((not self.is_last_in_row) or |
|---|
| 267 | n/a | (self.winfo_rootx() + self.winfo_width() < |
|---|
| 268 | n/a | page_set.winfo_rootx() + page_set.winfo_width()) |
|---|
| 269 | n/a | ): |
|---|
| 270 | n/a | # for a selected tab, if its rightmost edge isn't on the |
|---|
| 271 | n/a | # rightmost edge of the page set, the right mask should be one |
|---|
| 272 | n/a | # borderwidth shorter (vertically) |
|---|
| 273 | n/a | height -= self.bw |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | self.mskr.place(in_=self, |
|---|
| 276 | n/a | relx=1.0, x=0, |
|---|
| 277 | n/a | rely=1.0, y=0, |
|---|
| 278 | n/a | relwidth=0.0, width=self.bw, |
|---|
| 279 | n/a | relheight=0.0, height=height) |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | self.mskr.mr.place(x=-self.bw, y=-self.bw, |
|---|
| 282 | n/a | width=2*self.bw, height=height + self.bw*2) |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | # finally, lower the tab set so that all of the frames we just |
|---|
| 285 | n/a | # placed hide it |
|---|
| 286 | n/a | self.tab_set.lower() |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | class TabbedPageSet(Frame): |
|---|
| 290 | n/a | """A Tkinter tabbed-pane widget. |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | Constains set of 'pages' (or 'panes') with tabs above for selecting which |
|---|
| 293 | n/a | page is displayed. Only one page will be displayed at a time. |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | Pages may be accessed through the 'pages' attribute, which is a dictionary |
|---|
| 296 | n/a | of pages, using the name given as the key. A page is an instance of a |
|---|
| 297 | n/a | subclass of Tk's Frame widget. |
|---|
| 298 | n/a | |
|---|
| 299 | n/a | The page widgets will be created (and destroyed when required) by the |
|---|
| 300 | n/a | TabbedPageSet. Do not call the page's pack/place/grid/destroy methods. |
|---|
| 301 | n/a | |
|---|
| 302 | n/a | Pages may be added or removed at any time using the add_page() and |
|---|
| 303 | n/a | remove_page() methods. |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | """ |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | class Page(object): |
|---|
| 308 | n/a | """Abstract base class for TabbedPageSet's pages. |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | Subclasses must override the _show() and _hide() methods. |
|---|
| 311 | n/a | |
|---|
| 312 | n/a | """ |
|---|
| 313 | n/a | uses_grid = False |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | def __init__(self, page_set): |
|---|
| 316 | n/a | self.frame = Frame(page_set, borderwidth=2, relief=RAISED) |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | def _show(self): |
|---|
| 319 | n/a | raise NotImplementedError |
|---|
| 320 | n/a | |
|---|
| 321 | n/a | def _hide(self): |
|---|
| 322 | n/a | raise NotImplementedError |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | class PageRemove(Page): |
|---|
| 325 | n/a | """Page class using the grid placement manager's "remove" mechanism.""" |
|---|
| 326 | n/a | uses_grid = True |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | def _show(self): |
|---|
| 329 | n/a | self.frame.grid(row=0, column=0, sticky=NSEW) |
|---|
| 330 | n/a | |
|---|
| 331 | n/a | def _hide(self): |
|---|
| 332 | n/a | self.frame.grid_remove() |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | class PageLift(Page): |
|---|
| 335 | n/a | """Page class using the grid placement manager's "lift" mechanism.""" |
|---|
| 336 | n/a | uses_grid = True |
|---|
| 337 | n/a | |
|---|
| 338 | n/a | def __init__(self, page_set): |
|---|
| 339 | n/a | super(TabbedPageSet.PageLift, self).__init__(page_set) |
|---|
| 340 | n/a | self.frame.grid(row=0, column=0, sticky=NSEW) |
|---|
| 341 | n/a | self.frame.lower() |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | def _show(self): |
|---|
| 344 | n/a | self.frame.lift() |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | def _hide(self): |
|---|
| 347 | n/a | self.frame.lower() |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | class PagePackForget(Page): |
|---|
| 350 | n/a | """Page class using the pack placement manager's "forget" mechanism.""" |
|---|
| 351 | n/a | def _show(self): |
|---|
| 352 | n/a | self.frame.pack(fill=BOTH, expand=True) |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | def _hide(self): |
|---|
| 355 | n/a | self.frame.pack_forget() |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | def __init__(self, parent, page_names=None, page_class=PageLift, |
|---|
| 358 | n/a | n_rows=1, max_tabs_per_row=5, expand_tabs=False, |
|---|
| 359 | n/a | **kw): |
|---|
| 360 | n/a | """Constructor arguments: |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | page_names -- A list of strings, each will be the dictionary key to a |
|---|
| 363 | n/a | page's widget, and the name displayed on the page's tab. Should be |
|---|
| 364 | n/a | specified in the desired page order. The first page will be the default |
|---|
| 365 | n/a | and first active page. If page_names is None or empty, the |
|---|
| 366 | n/a | TabbedPageSet will be initialized empty. |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | n_rows, max_tabs_per_row -- Parameters for the TabSet which will |
|---|
| 369 | n/a | manage the tabs. See TabSet's docs for details. |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | page_class -- Pages can be shown/hidden using three mechanisms: |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | * PageLift - All pages will be rendered one on top of the other. When |
|---|
| 374 | n/a | a page is selected, it will be brought to the top, thus hiding all |
|---|
| 375 | n/a | other pages. Using this method, the TabbedPageSet will not be resized |
|---|
| 376 | n/a | when pages are switched. (It may still be resized when pages are |
|---|
| 377 | n/a | added/removed.) |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | * PageRemove - When a page is selected, the currently showing page is |
|---|
| 380 | n/a | hidden, and the new page shown in its place. Using this method, the |
|---|
| 381 | n/a | TabbedPageSet may resize when pages are changed. |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | * PagePackForget - This mechanism uses the pack placement manager. |
|---|
| 384 | n/a | When a page is shown it is packed, and when it is hidden it is |
|---|
| 385 | n/a | unpacked (i.e. pack_forget). This mechanism may also cause the |
|---|
| 386 | n/a | TabbedPageSet to resize when the page is changed. |
|---|
| 387 | n/a | |
|---|
| 388 | n/a | """ |
|---|
| 389 | n/a | Frame.__init__(self, parent, **kw) |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | self.page_class = page_class |
|---|
| 392 | n/a | self.pages = {} |
|---|
| 393 | n/a | self._pages_order = [] |
|---|
| 394 | n/a | self._current_page = None |
|---|
| 395 | n/a | self._default_page = None |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | self.columnconfigure(0, weight=1) |
|---|
| 398 | n/a | self.rowconfigure(1, weight=1) |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | self.pages_frame = Frame(self) |
|---|
| 401 | n/a | self.pages_frame.grid(row=1, column=0, sticky=NSEW) |
|---|
| 402 | n/a | if self.page_class.uses_grid: |
|---|
| 403 | n/a | self.pages_frame.columnconfigure(0, weight=1) |
|---|
| 404 | n/a | self.pages_frame.rowconfigure(0, weight=1) |
|---|
| 405 | n/a | |
|---|
| 406 | n/a | # the order of the following commands is important |
|---|
| 407 | n/a | self._tab_set = TabSet(self, self.change_page, n_rows=n_rows, |
|---|
| 408 | n/a | max_tabs_per_row=max_tabs_per_row, |
|---|
| 409 | n/a | expand_tabs=expand_tabs) |
|---|
| 410 | n/a | if page_names: |
|---|
| 411 | n/a | for name in page_names: |
|---|
| 412 | n/a | self.add_page(name) |
|---|
| 413 | n/a | self._tab_set.grid(row=0, column=0, sticky=NSEW) |
|---|
| 414 | n/a | |
|---|
| 415 | n/a | self.change_page(self._default_page) |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | def add_page(self, page_name): |
|---|
| 418 | n/a | """Add a new page with the name given in page_name.""" |
|---|
| 419 | n/a | if not page_name: |
|---|
| 420 | n/a | raise InvalidNameError("Invalid TabPage name: '%s'" % page_name) |
|---|
| 421 | n/a | if page_name in self.pages: |
|---|
| 422 | n/a | raise AlreadyExistsError( |
|---|
| 423 | n/a | "TabPage named '%s' already exists" % page_name) |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | self.pages[page_name] = self.page_class(self.pages_frame) |
|---|
| 426 | n/a | self._pages_order.append(page_name) |
|---|
| 427 | n/a | self._tab_set.add_tab(page_name) |
|---|
| 428 | n/a | |
|---|
| 429 | n/a | if len(self.pages) == 1: # adding first page |
|---|
| 430 | n/a | self._default_page = page_name |
|---|
| 431 | n/a | self.change_page(page_name) |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | def remove_page(self, page_name): |
|---|
| 434 | n/a | """Destroy the page whose name is given in page_name.""" |
|---|
| 435 | n/a | if not page_name in self.pages: |
|---|
| 436 | n/a | raise KeyError("No such TabPage: '%s" % page_name) |
|---|
| 437 | n/a | |
|---|
| 438 | n/a | self._pages_order.remove(page_name) |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | # handle removing last remaining, default, or currently shown page |
|---|
| 441 | n/a | if len(self._pages_order) > 0: |
|---|
| 442 | n/a | if page_name == self._default_page: |
|---|
| 443 | n/a | # set a new default page |
|---|
| 444 | n/a | self._default_page = self._pages_order[0] |
|---|
| 445 | n/a | else: |
|---|
| 446 | n/a | self._default_page = None |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | if page_name == self._current_page: |
|---|
| 449 | n/a | self.change_page(self._default_page) |
|---|
| 450 | n/a | |
|---|
| 451 | n/a | self._tab_set.remove_tab(page_name) |
|---|
| 452 | n/a | page = self.pages.pop(page_name) |
|---|
| 453 | n/a | page.frame.destroy() |
|---|
| 454 | n/a | |
|---|
| 455 | n/a | def change_page(self, page_name): |
|---|
| 456 | n/a | """Show the page whose name is given in page_name.""" |
|---|
| 457 | n/a | if self._current_page == page_name: |
|---|
| 458 | n/a | return |
|---|
| 459 | n/a | if page_name is not None and page_name not in self.pages: |
|---|
| 460 | n/a | raise KeyError("No such TabPage: '%s'" % page_name) |
|---|
| 461 | n/a | |
|---|
| 462 | n/a | if self._current_page is not None: |
|---|
| 463 | n/a | self.pages[self._current_page]._hide() |
|---|
| 464 | n/a | self._current_page = None |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | if page_name is not None: |
|---|
| 467 | n/a | self._current_page = page_name |
|---|
| 468 | n/a | self.pages[page_name]._show() |
|---|
| 469 | n/a | |
|---|
| 470 | n/a | self._tab_set.set_selected_tab(page_name) |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | |
|---|
| 473 | n/a | def _tabbed_pages(parent): # htest # |
|---|
| 474 | n/a | top=Toplevel(parent) |
|---|
| 475 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
|---|
| 476 | n/a | top.geometry("+%d+%d" % (x, y + 175)) |
|---|
| 477 | n/a | top.title("Test tabbed pages") |
|---|
| 478 | n/a | tabPage=TabbedPageSet(top, page_names=['Foobar','Baz'], n_rows=0, |
|---|
| 479 | n/a | expand_tabs=False, |
|---|
| 480 | n/a | ) |
|---|
| 481 | n/a | tabPage.pack(side=TOP, expand=TRUE, fill=BOTH) |
|---|
| 482 | n/a | Label(tabPage.pages['Foobar'].frame, text='Foo', pady=20).pack() |
|---|
| 483 | n/a | Label(tabPage.pages['Foobar'].frame, text='Bar', pady=20).pack() |
|---|
| 484 | n/a | Label(tabPage.pages['Baz'].frame, text='Baz').pack() |
|---|
| 485 | n/a | entryPgName=Entry(top) |
|---|
| 486 | n/a | buttonAdd=Button(top, text='Add Page', |
|---|
| 487 | n/a | command=lambda:tabPage.add_page(entryPgName.get())) |
|---|
| 488 | n/a | buttonRemove=Button(top, text='Remove Page', |
|---|
| 489 | n/a | command=lambda:tabPage.remove_page(entryPgName.get())) |
|---|
| 490 | n/a | labelPgName=Label(top, text='name of page to add/remove:') |
|---|
| 491 | n/a | buttonAdd.pack(padx=5, pady=5) |
|---|
| 492 | n/a | buttonRemove.pack(padx=5, pady=5) |
|---|
| 493 | n/a | labelPgName.pack(padx=5) |
|---|
| 494 | n/a | entryPgName.pack(padx=5) |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | if __name__ == '__main__': |
|---|
| 497 | n/a | from idlelib.idle_test.htest import run |
|---|
| 498 | n/a | run(_tabbed_pages) |
|---|