title
= 'Pmw.NoteBook demonstration (more complex)'
# Import Pmw from this directory tree.
sys
.path
[:0] = ['../../..']
def __init__(self
, parent
, withTabs
= 1):
# Repeat random number sequence for each run.
# Default demo is to display a tabbed notebook.
# Create a frame to put everything in
self
.mainframe
= Tkinter
.Frame(parent
)
self
.mainframe
.pack(fill
= 'both', expand
= 1)
# Find current default colors
button
= Tkinter
.Button()
defaultbg
= button
.cget('background')
defaultfg
= button
.cget('foreground')
# Create the list of colors to cycle through
self
.colorList
.append((defaultbg
, defaultfg
))
for color
in Pmw
.Color
.spectrum(6, 1.5, 1.0, 1.0, 1):
bg
= Pmw
.Color
.changebrightness(self
.mainframe
, color
, 0.85)
self
.colorList
.append((bg
, 'black'))
bg
= Pmw
.Color
.changebrightness(self
.mainframe
, color
, 0.55)
self
.colorList
.append((bg
, 'white'))
# Set the color to the current default
Pmw
.Color
.changecolor(self
.mainframe
, defaultbg
, foreground
= defaultfg
)
defaultPalette
= Pmw
.Color
.getdefaultpalette(self
.mainframe
)
Pmw
.Color
.setscheme(self
.mainframe
, defaultbg
, foreground
= defaultfg
)
# Create the notebook, but don't pack it yet.
self
.notebook
= Pmw
.NoteBook(self
.mainframe
,
createcommand
= PrintOne('Create'),
lowercommand
= PrintOne('Lower'),
raisecommand
= PrintOne('Raise'),
# Create a buttonbox to configure the notebook and pack it first.
buttonbox
= Pmw
.ButtonBox(self
.mainframe
)
buttonbox
.pack(side
= 'bottom', fill
= 'x')
# Add some buttons to the buttonbox to configure the notebook.
buttonbox
.add('Insert\npage', command
= self
.insertpage
)
buttonbox
.add('Delete\npage', command
= self
.deletepage
)
buttonbox
.add('Add\nbutton', command
= self
.addbutton
)
buttonbox
.add('Change\ncolor', command
= self
.changecolor
)
buttonbox
.add('Natural\nsize', command
=
self
.notebook
.setnaturalsize
)
# Create the selection widget to select the page in the notebook.
self
.optionmenu
= Pmw
.OptionMenu(self
.mainframe
,
command
= self
.notebook
.selectpage
self
.optionmenu
.pack(side
= 'left', padx
= 10)
# Pack the notebook last so that the buttonbox does not disappear
# when the window is made smaller.
self
.notebook
.pack(fill
= 'both', expand
= 1, padx
= 5, pady
= 5)
# Populate some pages of the notebook.
page
= self
.notebook
.add('tmp')
self
.notebook
.delete('tmp')
page
= self
.notebook
.add('Appearance')
self
.notebook
.tab('Appearance').focus_set()
button
= Tkinter
.Button(page
,
text
= 'Welcome\nto\nthe\nAppearance\npage')
page
= self
.notebook
.add('Fonts')
button
= Tkinter
.Button(page
,
text
= 'This is a very very very very wide Fonts page')
page
= self
.notebook
.insert('Applications', before
= 'Fonts')
button
= Tkinter
.Button(page
, text
= 'This is the Applications page')
# Initialise the first page and the initial colour.
self
.optionmenu
.setitems(self
.notebook
.pagenames())
apply(Pmw
.Color
.setscheme
, (self
.mainframe
,), defaultPalette
)
# Create a page at a random position
defaultPalette
= Pmw
.Color
.getdefaultpalette(self
.mainframe
)
bg
, fg
= self
.colorList
[self
.colorIndex
]
Pmw
.Color
.setscheme(self
.mainframe
, bg
, foreground
= fg
)
self
.pageCounter
= self
.pageCounter
+ 1
before
= self
.randomchoice(self
.notebook
.pagenames() + [Pmw
.END
])
pageName
= 'page%d' % self
.pageCounter
if self
.pageCounter
% 5 == 0:
tab_text
= pageName
+ '\nline two'
classes
= (None, Tkinter
.Button
, Tkinter
.Label
, Tkinter
.Checkbutton
)
cls
= self
.randomchoice((None,) + classes
)
print 'Adding', pageName
, 'as a frame with a button'
page
= self
.notebook
.insert(pageName
, before
, tab_text
= tab_text
)
page
= self
.notebook
.insert(pageName
, before
)
button
= Tkinter
.Button(page
,
text
= 'This is button %d' % self
.pageCounter
)
print 'Adding', pageName
, 'using', cls
page
= self
.notebook
.insert(pageName
, before
,
page_text
= 'This is a page using\na %s' % str(cls
)
page
= self
.notebook
.insert(pageName
, before
,
page_text
= 'This is a page using\na %s' % str(cls
)
self
.optionmenu
.setitems(
self
.notebook
.pagenames(), self
.notebook
.getcurselection())
apply(Pmw
.Color
.setscheme
, (self
.mainframe
,), defaultPalette
)
# Add a button to a random page.
defaultPalette
= Pmw
.Color
.getdefaultpalette(self
.mainframe
)
bg
, fg
= self
.colorList
[self
.colorIndex
]
Pmw
.Color
.setscheme(self
.mainframe
, bg
, foreground
= fg
)
for pageName
in self
.notebook
.pagenames():
page
= self
.notebook
.page(pageName
)
if page
.__class
__ == Tkinter
.Frame
:
framePages
.append(pageName
)
pageName
= self
.randomchoice(framePages
)
print 'Adding extra button to', pageName
page
= self
.notebook
.page(pageName
)
button
= Tkinter
.Button(page
, text
= 'This is an extra button')
apply(Pmw
.Color
.setscheme
, (self
.mainframe
,), defaultPalette
)
pageNames
= self
.notebook
.pagenames()
pageName
= self
.randomchoice(pageNames
)
print 'Deleting', pageName
self
.notebook
.delete(pageName
)
self
.optionmenu
.setitems(
self
.notebook
.pagenames(), self
.notebook
.getcurselection())
self
.colorIndex
= self
.colorIndex
+ 1
if self
.colorIndex
== len(self
.colorList
):
bg
, fg
= self
.colorList
[self
.colorIndex
]
print 'Changing color to', bg
Pmw
.Color
.changecolor(self
.mainframe
, bg
, foreground
= fg
)
self
.notebook
.recolorborders()
# Simple random number generator.
def randomchoice(self
, selection
):
self
.rand
= (self
.rand
* 125) % 2796203
def __init__(self
, text
):
def __call__(self
, text
):
######################################################################
# Create demo in root window for testing.
if __name__
== '__main__':
exitButton
= Tkinter
.Button(root
, text
= 'Exit', command
= root
.destroy
)