Skip to content

Instantly share code, notes, and snippets.

@mikofski
Last active November 12, 2020 09:06
Show Gist options
  • Save mikofski/5851633 to your computer and use it in GitHub Desktop.
Save mikofski/5851633 to your computer and use it in GitHub Desktop.
Python 2 version of ttk Notebook Demo by courtesy of Jane
#! /usr/bin/env python
from Tkinter import *
from ttk import *
root = Tk() # create a top-level window
master = Frame(root, name='master') # create Frame in "root"
master.pack(fill=BOTH) # fill both sides of the parent
root.title('EZ') # title for top-level window
# quit if the window is deleted
root.protocol("WM_DELETE_WINDOW", master.quit)
nb = Notebook(master, name='nb') # create Notebook in "master"
nb.pack(fill=BOTH, padx=2, pady=3) # fill "master" but pad sides
# create each Notebook tab in a Frame
master_foo = Frame(nb, name='master-foo')
Label(master_foo, text="this is foo").pack(side=LEFT)
# Button to quit app on right
btn = Button(master_foo, text="foo", command=master.quit)
btn.pack(side=RIGHT)
nb.add(master_foo, text="foo") # add tab to Notebook
# repeat for each tab
master_bar = Frame(master, name='master-bar')
Label(master_bar, text="this is bar").pack(side=LEFT)
btn = Button(master_bar, text="bar", command=master.quit)
btn.pack(side=RIGHT)
nb.add(master_bar, text="bar")
# start the app
if __name__ == "__main__":
master.mainloop() # call master's Frame.mainloop() method.
#root.destroy() # if mainloop quits, destroy window
#! /usr/bin/env python
from Tkinter import *
from ttk import *
class NotebookDemo(Frame):
def __init__(self, isapp=True, name='notebookdemo'):
Frame.__init__(self, name=name)
self.pack(expand=Y, fill=BOTH)
self.master.title('Notebook Demo')
self.isapp = isapp
self._create_widgets()
def _create_widgets(self):
self._create_demo_panel()
def _create_demo_panel(self):
demoPanel = Frame(self, name='demo')
demoPanel.pack(side=TOP, fill=BOTH, expand=Y)
# create the notebook
nb = Notebook(demoPanel, name='notebook')
# extend bindings to top level window allowing
# CTRL+TAB - cycles thru tabs
# SHIFT+CTRL+TAB - previous tab
# ALT+K - select tab using mnemonic (K = underlined letter)
nb.enable_traversal()
nb.pack(fill=BOTH, expand=Y, padx=2, pady=3)
self._create_descrip_tab(nb)
self._create_disabled_tab(nb)
self._create_text_tab(nb)
def _create_descrip_tab(self, nb):
# frame to hold contentx
frame = Frame(nb, name='descrip')
# widgets to be displayed on 'Description' tab
msg = [
"Ttk is the new Tk themed widget set. One of the widgets ",
"it includes is the notebook widget, which provides a set ",
"of tabs that allow the selection of a group of panels, ",
"each with distinct content. They are a feature of many ",
"modern user interfaces. Not only can the tabs be selected ",
"with the mouse, but they can also be switched between ",
"using Ctrl+Tab when the notebook page heading itself is ",
"selected. Note that the second tab is disabled, and cannot "
"be selected."]
lbl = Label(frame, wraplength='4i', justify=LEFT, anchor=N,
text=''.join(msg))
neatVar = StringVar()
btn = Button(frame, text='Neat!', underline=0,
command=lambda v=neatVar: self._say_neat(v))
neat = Label(frame, textvariable=neatVar, name='neat')
# position and set resize behaviour
lbl.grid(row=0, column=0, columnspan=2, sticky='new', pady=5)
btn.grid(row=1, column=0, pady=(2,4))
neat.grid(row=1, column=1, pady=(2,4))
frame.rowconfigure(1, weight=1)
frame.columnconfigure((0,1), weight=1, uniform=1)
# bind for button short-cut key
# (must be bound to toplevel window)
self.winfo_toplevel().bind('<Alt-n>', lambda e, v=neatVar: self._say_neat(v))
# add to notebook (underline = index for short-cut character)
nb.add(frame, text='Description', underline=0, padding=2)
def _say_neat(self, v):
v.set('Yeah, I know...')
self.update()
self.after(500, v.set(''))
# =============================================================================
def _create_disabled_tab(self, nb):
# Populate the second pane. Note that the content doesn't really matter
frame = Frame(nb)
nb.add(frame, text='Disabled', state='disabled')
# =============================================================================
def _create_text_tab(self, nb):
# populate the third frame with a text widget
frame = Frame(nb)
txt = Text(frame, wrap=WORD, width=40, height=10)
vscroll = Scrollbar(frame, orient=VERTICAL, command=txt.yview)
txt['yscroll'] = vscroll.set
vscroll.pack(side=RIGHT, fill=Y)
txt.pack(fill=BOTH, expand=Y)
# add to notebook (underline = index for short-cut character)
nb.add(frame, text='Text Editor', underline=0)
if __name__ == '__main__':
NotebookDemo().mainloop()
#! /usr/bin/env python
from Tkinter import *
from ttk import *
# NOTE: Frame will make a top-level window if one doesn't already exist which
# can then be accessed via the frame's master attribute
# make a Frame whose parent is root, named "notebookdemo"
master = Frame(name='notebookdemo')
root = master.master # short-cut to top-level window
master.pack() # pack the Frame into root, defaults to side=TOP
root.title('Notebook Demo') # name the window
# create notebook
demoPanel = Frame(master, name='demo') # create a new frame slaved to master
demoPanel.pack() # pack the Frame into root
# create (notebook) demo panel
nb = Notebook(demoPanel, name='notebook') # create the ttk.Notebook widget
# extend bindings to top level window allowing
# CTRL+TAB - cycles thru tabs
# SHIFT+CTRL+TAB - previous tab
# ALT+K - select tab using mnemonic (K = underlined letter)
nb.enable_traversal()
nb.pack(fill=BOTH, expand=Y, padx=2, pady=3) # add margin
# create description tab
# frame to hold (tab) content
frame = Frame(nb, name='descrip')
# widgets to be displayed on 'Description' tab
msg = [
"Ttk is the new Tk themed widget set. One of the widgets ",
"it includes is the notebook widget, which provides a set ",
"of tabs that allow the selection of a group of panels, ",
"each with distinct content. They are a feature of many ",
"modern user interfaces. Not only can the tabs be selected ",
"with the mouse, but they can also be switched between ",
"using Ctrl+Tab when the notebook page heading itself is ",
"selected. Note that the second tab is disabled, and cannot "
"be selected."]
lbl = Label(frame, wraplength='4i', justify=LEFT, anchor=N,
text=''.join(msg))
neatVar = StringVar()
btn = Button(frame, text='Neat!', underline=0,
command=lambda v=neatVar: _say_neat(master, v))
neat = Label(frame, textvariable=neatVar, name='neat')
# position and set resize behavior
lbl.grid(row=0, column=0, columnspan=2, sticky='new', pady=5)
btn.grid(row=1, column=0, pady=(2,4))
neat.grid(row=1, column=1, pady=(2,4))
frame.rowconfigure(1, weight=1)
frame.columnconfigure((0,1), weight=1, uniform=1)
# bind for button short-cut key
# (must be bound to toplevel window)
master.winfo_toplevel().bind('<Alt-n>', lambda e, v=neatVar: self._say_neat(v))
# add to notebook (underline = index for short-cut character)
nb.add(frame, text='Description', underline=0, padding=2)
def _say_neat(master, v):
v.set('Yeah, I know...')
master.update()
master.after(500, v.set(''))
# create disabled tab
# Populate the second pane. Note that the content doesn't really matter
disabled_frame = Frame(nb)
nb.add(disabled_frame, text='Disabled', state='disabled')
# create text tab
# populate the third frame with a text widget
txt_frame = Frame(nb)
txt = Text(txt_frame, wrap=WORD, width=40, height=10)
vscroll = Scrollbar(txt_frame, orient=VERTICAL, command=txt.yview)
txt['yscroll'] = vscroll.set
vscroll.pack(side=RIGHT, fill=Y)
txt.pack(fill=BOTH, expand=Y)
# add to notebook (underline = index for short-cut character)
nb.add(txt_frame, text='Text Editor', underline=0)
master.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment