| 35 | |
| 36 | # the bulk of the logic is in the actual tab bar |
| 37 | class TabBar(Frame): |
| 38 | def __init__(self, master=None, init_name=None): |
| 39 | Frame.__init__(self, master) |
| 40 | self.tabs = {} |
| 41 | self.buttons = {} |
| 42 | self.current_tab = None |
| 43 | self.init_name = init_name |
| 44 | |
| 45 | def show(self): |
| 46 | self.pack(side=TOP, expand=YES, fill=X) |
| 47 | self.switch_tab(self.init_name or self.tabs.keys()[-1])# switch the tab to the first tab |
| 48 | |
| 49 | def add(self, tab): |
| 50 | tab.pack_forget() # hide the tab on init |
| 51 | |
| 52 | self.tabs[tab.tab_name] = tab # add it to the list of tabs |
| 53 | b = Button(self, text=tab.tab_name, relief=BASE, # basic button stuff |
| 54 | command=(lambda name=tab.tab_name: self.switch_tab(name))) # set the command to switch tabs |
| 55 | b.pack(side=LEFT) # pack the buttont to the left mose of self |
| 56 | self.buttons[tab.tab_name] = b # add it to the list of buttons |
| 57 | |
| 58 | def delete(self, tabname): |
| 59 | |
| 60 | if tabname == self.current_tab: |
| 61 | self.current_tab = None |
| 62 | self.tabs[tabname].pack_forget() |
| 63 | del self.tabs[tabname] |
| 64 | self.switch_tab(self.tabs.keys()[0]) |
| 65 | |
| 66 | else: del self.tabs[tabname] |
| 67 | |
| 68 | self.buttons[tabname].pack_forget() |
| 69 | del self.buttons[tabname] |
| 70 | |
| 71 | |
| 72 | def switch_tab(self, name): |
| 73 | if self.current_tab: |
| 74 | self.buttons[self.current_tab].config(relief=BASE) |
| 75 | self.tabs[self.current_tab].pack_forget() # hide the current tab |
| 76 | self.tabs[name].pack(side=BOTTOM) # add the new tab to the display |
| 77 | self.current_tab = name # set the current tab to itself |
| 78 | |
| 79 | self.buttons[name].config(relief=SELECTED) # set it to the selected style |
| 80 | |
| 81 | if __name__ == '__main__': |
| 82 | def write(x): print x |