1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
title = 'Pmw.NoteBook demonstration (more complex)'
# Import Pmw from this directory tree.
import sys
sys.path[:0] = ['../../..']
import Tkinter
import Pmw
class Demo:
def __init__(self, parent, withTabs = 1):
# Repeat random number sequence for each run.
self.rand = 12345
# Default demo is to display a tabbed notebook.
self.withTabs = withTabs
# 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')
button.destroy()
# Create the list of colors to cycle through
self.colorList = []
self.colorList.append((defaultbg, defaultfg))
self.colorIndex = 0
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.
if self.withTabs:
tabpos = 'n'
else:
tabpos = None
self.notebook = Pmw.NoteBook(self.mainframe,
tabpos = tabpos,
createcommand = PrintOne('Create'),
lowercommand = PrintOne('Lower'),
raisecommand = PrintOne('Raise'),
hull_width = 300,
hull_height = 200,
)
# 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)
if not self.withTabs:
# Create the selection widget to select the page in the notebook.
self.optionmenu = Pmw.OptionMenu(self.mainframe,
menubutton_width = 10,
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')
if self.withTabs:
self.notebook.tab('Appearance').focus_set()
button = Tkinter.Button(page,
text = 'Welcome\nto\nthe\nAppearance\npage')
button.pack(expand = 1)
page = self.notebook.add('Fonts')
button = Tkinter.Button(page,
text = 'This is a very very very very wide Fonts page')
button.pack(expand = 1)
page = self.notebook.insert('Applications', before = 'Fonts')
button = Tkinter.Button(page, text = 'This is the Applications page')
button.pack(expand = 1)
# Initialise the first page and the initial colour.
if not self.withTabs:
self.optionmenu.setitems(self.notebook.pagenames())
apply(Pmw.Color.setscheme, (self.mainframe,), defaultPalette)
self.pageCounter = 0
def insertpage(self):
# 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'
else:
tab_text = pageName
classes = (None, Tkinter.Button, Tkinter.Label, Tkinter.Checkbutton)
cls = self.randomchoice((None,) + classes)
if cls is None:
print 'Adding', pageName, 'as a frame with a button'
if self.withTabs:
page = self.notebook.insert(pageName, before, tab_text = tab_text)
else:
page = self.notebook.insert(pageName, before)
button = Tkinter.Button(page,
text = 'This is button %d' % self.pageCounter)
button.pack(expand = 1)
else:
print 'Adding', pageName, 'using', cls
if self.withTabs:
page = self.notebook.insert(pageName, before,
tab_text = tab_text,
page_pyclass = cls,
page_text = 'This is a page using\na %s' % str(cls)
)
else:
page = self.notebook.insert(pageName, before,
page_pyclass = cls,
page_text = 'This is a page using\na %s' % str(cls)
)
if not self.withTabs:
self.optionmenu.setitems(
self.notebook.pagenames(), self.notebook.getcurselection())
apply(Pmw.Color.setscheme, (self.mainframe,), defaultPalette)
def addbutton(self):
# 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)
framePages = []
for pageName in self.notebook.pagenames():
page = self.notebook.page(pageName)
if page.__class__ == Tkinter.Frame:
framePages.append(pageName)
if len(framePages) == 0:
self.notebook.bell()
return
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')
button.pack(expand = 1)
apply(Pmw.Color.setscheme, (self.mainframe,), defaultPalette)
def deletepage(self):
# Delete a random page
pageNames = self.notebook.pagenames()
if len(pageNames) == 0:
self.notebook.bell()
return
pageName = self.randomchoice(pageNames)
print 'Deleting', pageName
self.notebook.delete(pageName)
if not self.withTabs:
self.optionmenu.setitems(
self.notebook.pagenames(), self.notebook.getcurselection())
def changecolor(self):
self.colorIndex = self.colorIndex + 1
if self.colorIndex == len(self.colorList):
self.colorIndex = 0
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):
num = len(selection)
self.rand = (self.rand * 125) % 2796203
index = self.rand % num
return selection[index]
class PrintOne:
def __init__(self, text):
self.text = text
def __call__(self, text):
print self.text, text
######################################################################
# Create demo in root window for testing.
if __name__ == '__main__':
root = Tkinter.Tk()
Pmw.initialise(root, fontScheme = 'pmw1')
root.title(title)
widget = Demo(root)
exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
exitButton.pack()
root.mainloop()
|