|
1 | 1 | #!/usr/bin/env python
|
2 |
| -import Tkinter as tk |
| 2 | +import ttk |
| 3 | +import Tkinter |
| 4 | + |
| 5 | +class Wizard(object, ttk.Notebook): |
| 6 | + def __init__(self, master=None, **kw): |
| 7 | + npages = kw.pop('npages', 3) |
| 8 | + kw['style'] = 'Wizard.TNotebook' |
| 9 | + ttk.Style(master).layout('Wizard.TNotebook.Tab', '') |
| 10 | + ttk.Notebook.__init__(self, master, **kw) |
| 11 | + |
| 12 | + self._children = {} |
| 13 | + |
| 14 | + for page in range(npages): |
| 15 | + self.add_empty_page() |
| 16 | + |
| 17 | + self.current = 0 |
| 18 | + self._wizard_buttons() |
| 19 | + |
| 20 | + def _wizard_buttons(self): |
| 21 | + """Place wizard buttons in the pages.""" |
| 22 | + for indx, child in self._children.iteritems(): |
| 23 | + btnframe = ttk.Frame(child) |
| 24 | + btnframe.pack(side='bottom', fill='x', padx=6, pady=12) |
| 25 | + |
| 26 | + nextbtn = ttk.Button(btnframe, text="Next", command=self.next_page) |
| 27 | + nextbtn.pack(side='right', anchor='e', padx=6) |
| 28 | + if indx != 0: |
| 29 | + prevbtn = ttk.Button(btnframe, text="Previous", |
| 30 | + command=self.prev_page) |
| 31 | + prevbtn.pack(side='right', anchor='e', padx=6) |
| 32 | + |
| 33 | + if indx == len(self._children) - 1: |
| 34 | + nextbtn.configure(text="Finish", command=self.close) |
| 35 | + |
| 36 | + def next_page(self): |
| 37 | + self.current += 1 |
| 38 | + |
| 39 | + def prev_page(self): |
| 40 | + self.current -= 1 |
| 41 | + |
| 42 | + def close(self): |
| 43 | + self.master.destroy() |
| 44 | + |
| 45 | + def add_empty_page(self): |
| 46 | + child = ttk.Frame(self) |
| 47 | + self._children[len(self._children)] = child |
| 48 | + self.add(child) |
| 49 | + |
| 50 | + def add_page_body(self, body): |
| 51 | + body.pack(side='top', fill='both', padx=6, pady=12) |
| 52 | + |
| 53 | + def page_container(self, page_num): |
| 54 | + if page_num in self._children: |
| 55 | + return self._children[page_num] |
| 56 | + else: |
| 57 | + raise KeyError("Invalid page: %s" % page_num) |
| 58 | + |
| 59 | + def _get_current(self): |
| 60 | + return self._current |
| 61 | + |
| 62 | + def _set_current(self, curr): |
| 63 | + if curr not in self._children: |
| 64 | + raise KeyError("Invalid page: %s" % curr) |
| 65 | + |
| 66 | + self._current = curr |
| 67 | + self.select(self._children[self._current]) |
| 68 | + |
| 69 | + current = property(_get_current, _set_current) |
| 70 | + |
| 71 | +def welcome_page(root): |
| 72 | + page = root.page_container(0) |
| 73 | + title = ttk.Label(page, text='Periscope', font='bold 28').pack() |
| 74 | + subtitle = ttk.Label(page, text='Tor Censorship Detector', |
| 75 | + foreground='grey').pack() |
| 76 | + |
| 77 | +def demo(): |
| 78 | + root = Tkinter.Tk() |
| 79 | + root.title("Periscope") |
| 80 | + wizard = Wizard(npages=3) |
| 81 | + wizard.master.minsize(600, 400) |
| 82 | + welcome_page(wizard) |
| 83 | + page1 = ttk.Label(wizard.page_container(1), text='Page 2') |
| 84 | + page2 = ttk.Label(wizard.page_container(2), text='Page 3') |
| 85 | + wizard.add_page_body(page1) |
| 86 | + wizard.add_page_body(page2) |
| 87 | + wizard.pack(fill='both', expand=True) |
| 88 | + root.mainloop() |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + demo() |
3 | 92 |
|
0 commit comments