###################################################
# Tabbed interface script
# www.sunjay-varma.com
###################################################
__doc__ = info = '''
This script was written by Sunjay Varma - www.sunjay-varma.com
This script has two main classes:
Tab - Basic tab used by TabBar for main functionality
TabBar - The tab bar that is placed above tab bodies (Tabs)
It uses a pretty basic structure:
root
-->TabBar(root, init_name) (For switching tabs)
-->Tab (Place holder for content)
\t-->content (content of the tab; parent=Tab)
-->Tab (Place holder for content)
\t-->content (content of the tab; parent=Tab)
-->Tab (Place holder for content)
\t-->content (content of the tab; parent=Tab)
etc.
'''
from Tkinter import *
BASE = RAISED
SELECTED = FLAT
# a base tab class
class Tab(Frame):
def __init__(self, master, name):
Frame.__init__(self, master)
self.tab_name = name
# the bulk of the logic is in the actual tab bar
class TabBar(Frame):
def __init__(self, master=None, init_name=None):
Frame.__init__(self, master)
self.tabs = {}
self.buttons = []
self.current_tab = None
self.init_name = init_name
def show(self):
self.pack(side=TOP, expand=YES, fill=X)
self.tabs = self.tabs
self.switch_tab(self.init_name or self.tabs.keys()[0])
def add(self, tab):
tab.pack_forget() # hide the tab on init
self.tabs[tab.tab_name] = tab # add it to the list of tabs
b = Button(self, text=tab.tab_name, relief=BASE, # basic button stuff
command=(lambda name=tab.tab_name: self.switch_tab(name))) # set the command to switch tabs
b.pack(side=LEFT) # pack the buttont to the left mose of self
self.buttons.append(b) # add it to the list of buttons
def switch_tab(self, name):
if self.current_tab:
i = self.tabs.keys().index(self.current_tab)
self.buttons[-i-1].config(relief=BASE)
self.tabs[self.current_tab].pack_forget() # hide the current tab
self.tabs[name].pack(side=BOTTOM) # add the new tab to the display
self.current_tab = name # set the current tab to itself
i = self.tabs.keys().index(name) # get the current tab
self.buttons[-i-1].config(relief=SELECTED) # set it to the selected style
if __name__ == '__main__':
def write(x): print x
root = Tk()
root.title("Tabs")
bar = TabBar(root, "Info")
tab1 = Tab(root, "Wow...") # notice how this one's master is the root instead of the bar
Label(tab1, text="Sunjay Varma is an extra ordinary little boy.\n\n\n\n\nCheck out his website:\nwww.sunjay-varma.com", bg="white", fg="red").pack(side=TOP, expand=YES, fill=BOTH)
Button(tab1, text="PRESS ME!", command=(lambda: write("YOU PRESSED ME!"))).pack(side=BOTTOM, fill=BOTH, expand=YES)
tab2 = Tab(root, "Hi there!")
Label(tab2, text="How are you??", bg='black', fg='#3366ff').pack(side=TOP, fill=BOTH, expand=YES)
txt = Text(tab2, width=50, height=20)
txt.focus()
txt.pack(side=LEFT, fill=X, expand=YES)
Button(tab2, text="Get", command=(lambda: write(txt.get('1.0', END).strip()))).pack(side=BOTTOM, expand=YES, fill=BOTH)
tab3 = Tab(root, "Info")
Label(tab3, bg='white', text="This tab was given as an argument to the TabBar constructor.\n\nINFO:\n"+info).pack(side=LEFT, expand=YES, fill=BOTH)
bar.add(tab1)
bar.add(tab2)
bar.add(tab3)
#bar.config(bd=2, relief=RIDGE) # add some border
bar.show()
root.mainloop()
Diff to Previous Revision
--- revision 1 2010-06-13 01:27:17
+++ revision 2 2010-06-13 01:31:58
@@ -1,18 +1,18 @@
-<span class="mceItemHidden" spellcheck="false">###################################################
+###################################################
# Tabbed interface script
# www.sunjay-varma.com
###################################################
__doc__ = info = '''
-This script <span class="hiddenGrammarError" pre="script ">was written</span> by <span class="hiddenSpellError" pre="by ">Sunjay</span> <span class="hiddenSpellError" pre="Sunjay ">Varma</span> - www.sunjay-varma.com
+This script was written by Sunjay Varma - www.sunjay-varma.com
This script has two main classes:
-Tab - Basic tab used by <span class="hiddenSpellError" pre="by "><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">TabBar</span></span></span> <span class="hiddenSpellError" pre="TabBar ">for</span> main functionality
-<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">TabBar</span></span></span> - The tab bar that <span class="hiddenGrammarError" pre="that ">is placed</span> above tab bodies (Tabs)
+Tab - Basic tab used by TabBar for main functionality
+TabBar - The tab bar that is placed above tab bodies (Tabs)
It uses a pretty basic structure:
root
---><span class="hiddenSpellError" pre="">TabBar</span>(root, init_name) (For switching tabs)
+-->TabBar(root, init_name) (For switching tabs)
-->Tab (Place holder for content)
\t-->content (content of the tab; parent=Tab)
-->Tab (Place holder for content)
@@ -22,7 +22,7 @@
etc.
'''
-from <span class="hiddenSpellError" pre="from ">Tkinter</span> import *
+from Tkinter import *
BASE = RAISED
SELECTED = FLAT
@@ -33,8 +33,8 @@
Frame.__init__(self, master)
self.tab_name = name
-# the bulk of the logic is in the <span class="hiddenSuggestion" pre="the ">actual</span> tab bar
-class <span class="hiddenSpellError" pre="class ">TabBar</span>(Frame):
+# the bulk of the logic is in the actual tab bar
+class TabBar(Frame):
def __init__(self, master=None, init_name=None):
Frame.__init__(self, master)
self.tabs = {}
@@ -54,19 +54,19 @@
self.tabs[tab.tab_name] = tab # add it to the list of tabs
b = Button(self, text=tab.tab_name, relief=BASE, # basic button stuff
command=(lambda name=tab.tab_name: self.switch_tab(name))) # set the command to switch tabs
- b.pack(side=LEFT) # pack the <span class="hiddenSpellError" pre="the ">buttont</span> to the left <span class="hiddenSpellError" pre="left ">mose</span> of self
+ b.pack(side=LEFT) # pack the buttont to the left mose of self
self.buttons.append(b) # add it to the list of buttons
def switch_tab(self, name):
if self.current_tab:
- <span class="hiddenGrammarError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenGrammarError" pre="">i</span></span></span> = self.tabs.keys().index(self.current_tab)
- self.buttons[<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">-i-1</span></span></span>].<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">config</span></span></span>(relief=BASE)
+ i = self.tabs.keys().index(self.current_tab)
+ self.buttons[-i-1].config(relief=BASE)
self.tabs[self.current_tab].pack_forget() # hide the current tab
self.tabs[name].pack(side=BOTTOM) # add the new tab to the display
self.current_tab = name # set the current tab to itself
- <span class="hiddenGrammarError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenGrammarError" pre="">i</span></span></span> = self.tabs.keys().index(name) # get the current tab
- self.buttons[<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">-i-1</span></span></span>].<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">config</span></span></span>(relief=SELECTED) # set it to the selected style
+ i = self.tabs.keys().index(name) # get the current tab
+ self.buttons[-i-1].config(relief=SELECTED) # set it to the selected style
if __name__ == '__main__':
@@ -75,30 +75,28 @@
root = Tk()
root.title("Tabs")
- bar = <span class="hiddenSpellError" pre="bar ">TabBar</span>(root, "Info")
+ bar = TabBar(root, "Info")
- <span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab1</span></span></span> = Tab(root, "Wow...") # notice how this one's master is the root instead of the bar
- Label(<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab1</span></span></span><span class="mceItemHidden" spellcheck="false">, text="Sunjay <span class="hiddenSpellError" pre="Sunjay ">Varma</span> is an extra ordinary little boy.\n\n\n\n\nCheck out his website:\nwww.sunjay-varma.com", bg="white", fg="red").pack(side=TOP, expand=YES, fill=BOTH)
- Button(</span><span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab1</span></span></span>, text="PRESS ME!", command=(lambda: write("YOU PRESSED ME!"))).pack(side=BOTTOM, fill=BOTH, expand=YES)
+ tab1 = Tab(root, "Wow...") # notice how this one's master is the root instead of the bar
+ Label(tab1, text="Sunjay Varma is an extra ordinary little boy.\n\n\n\n\nCheck out his website:\nwww.sunjay-varma.com", bg="white", fg="red").pack(side=TOP, expand=YES, fill=BOTH)
+ Button(tab1, text="PRESS ME!", command=(lambda: write("YOU PRESSED ME!"))).pack(side=BOTTOM, fill=BOTH, expand=YES)
- <span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab2</span></span></span> = Tab(root, "Hi there!")
- Label(<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab2</span></span></span>, text="How are you??", bg='black', fg='#3366ff').pack(side=TOP, fill=BOTH, expand=YES)
- txt = Text(<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab2</span></span></span>, width=50, height=20)
+ tab2 = Tab(root, "Hi there!")
+ Label(tab2, text="How are you??", bg='black', fg='#3366ff').pack(side=TOP, fill=BOTH, expand=YES)
+ txt = Text(tab2, width=50, height=20)
txt.focus()
txt.pack(side=LEFT, fill=X, expand=YES)
- Button(<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab2</span></span></span>, text="Get", command=(lambda: write(txt.get('1.0', END).strip()))).pack(side=BOTTOM, expand=YES, fill=BOTH)
+ Button(tab2, text="Get", command=(lambda: write(txt.get('1.0', END).strip()))).pack(side=BOTTOM, expand=YES, fill=BOTH)
- <span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab3</span></span></span> = Tab(root, "Info")
- Label(<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab3</span></span></span>, bg='white', text="This tab was given as an argument to the <span class="hiddenSpellError" pre="the ">TabBar</span> constructor.\n\nINFO:\n"+info).pack(side=LEFT, expand=YES, fill=BOTH)
+ tab3 = Tab(root, "Info")
+ Label(tab3, bg='white', text="This tab was given as an argument to the TabBar constructor.\n\nINFO:\n"+info).pack(side=LEFT, expand=YES, fill=BOTH)
- bar.add(<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab1</span></span></span>)
- bar.add(<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab2</span></span></span>)
- bar.add(<span class="hiddenSpellError" pre=""><span class="mceItemHidden" spellcheck="false"><span class="hiddenSpellError" pre="">tab3</span></span></span>)
+ bar.add(tab1)
+ bar.add(tab2)
+ bar.add(tab3)
#bar.config(bd=2, relief=RIDGE) # add some border
bar.show()
root.mainloop()
-
-</span>