Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / lib / python2.4 / site-packages / Pmw / Pmw_1_2 / demos / NoteBook_2.py
CommitLineData
920dae64
AT
1title = 'Pmw.NoteBook demonstration (more complex)'
2
3# Import Pmw from this directory tree.
4import sys
5sys.path[:0] = ['../../..']
6
7import Tkinter
8import Pmw
9
10class Demo:
11 def __init__(self, parent, withTabs = 1):
12
13 # Repeat random number sequence for each run.
14 self.rand = 12345
15
16 # Default demo is to display a tabbed notebook.
17 self.withTabs = withTabs
18
19 # Create a frame to put everything in
20 self.mainframe = Tkinter.Frame(parent)
21 self.mainframe.pack(fill = 'both', expand = 1)
22
23 # Find current default colors
24 button = Tkinter.Button()
25 defaultbg = button.cget('background')
26 defaultfg = button.cget('foreground')
27 button.destroy()
28
29 # Create the list of colors to cycle through
30 self.colorList = []
31 self.colorList.append((defaultbg, defaultfg))
32 self.colorIndex = 0
33 for color in Pmw.Color.spectrum(6, 1.5, 1.0, 1.0, 1):
34 bg = Pmw.Color.changebrightness(self.mainframe, color, 0.85)
35 self.colorList.append((bg, 'black'))
36 bg = Pmw.Color.changebrightness(self.mainframe, color, 0.55)
37 self.colorList.append((bg, 'white'))
38
39 # Set the color to the current default
40 Pmw.Color.changecolor(self.mainframe, defaultbg, foreground = defaultfg)
41 defaultPalette = Pmw.Color.getdefaultpalette(self.mainframe)
42 Pmw.Color.setscheme(self.mainframe, defaultbg, foreground = defaultfg)
43
44 # Create the notebook, but don't pack it yet.
45 if self.withTabs:
46 tabpos = 'n'
47 else:
48 tabpos = None
49 self.notebook = Pmw.NoteBook(self.mainframe,
50 tabpos = tabpos,
51 createcommand = PrintOne('Create'),
52 lowercommand = PrintOne('Lower'),
53 raisecommand = PrintOne('Raise'),
54 hull_width = 300,
55 hull_height = 200,
56 )
57
58 # Create a buttonbox to configure the notebook and pack it first.
59 buttonbox = Pmw.ButtonBox(self.mainframe)
60 buttonbox.pack(side = 'bottom', fill = 'x')
61
62 # Add some buttons to the buttonbox to configure the notebook.
63 buttonbox.add('Insert\npage', command = self.insertpage)
64 buttonbox.add('Delete\npage', command = self.deletepage)
65 buttonbox.add('Add\nbutton', command = self.addbutton)
66 buttonbox.add('Change\ncolor', command = self.changecolor)
67 buttonbox.add('Natural\nsize', command =
68 self.notebook.setnaturalsize)
69
70 if not self.withTabs:
71 # Create the selection widget to select the page in the notebook.
72 self.optionmenu = Pmw.OptionMenu(self.mainframe,
73 menubutton_width = 10,
74 command = self.notebook.selectpage
75 )
76 self.optionmenu.pack(side = 'left', padx = 10)
77
78 # Pack the notebook last so that the buttonbox does not disappear
79 # when the window is made smaller.
80 self.notebook.pack(fill = 'both', expand = 1, padx = 5, pady = 5)
81
82 # Populate some pages of the notebook.
83 page = self.notebook.add('tmp')
84 self.notebook.delete('tmp')
85 page = self.notebook.add('Appearance')
86 if self.withTabs:
87 self.notebook.tab('Appearance').focus_set()
88 button = Tkinter.Button(page,
89 text = 'Welcome\nto\nthe\nAppearance\npage')
90 button.pack(expand = 1)
91 page = self.notebook.add('Fonts')
92 button = Tkinter.Button(page,
93 text = 'This is a very very very very wide Fonts page')
94 button.pack(expand = 1)
95 page = self.notebook.insert('Applications', before = 'Fonts')
96 button = Tkinter.Button(page, text = 'This is the Applications page')
97 button.pack(expand = 1)
98
99 # Initialise the first page and the initial colour.
100 if not self.withTabs:
101 self.optionmenu.setitems(self.notebook.pagenames())
102 apply(Pmw.Color.setscheme, (self.mainframe,), defaultPalette)
103 self.pageCounter = 0
104
105 def insertpage(self):
106 # Create a page at a random position
107
108 defaultPalette = Pmw.Color.getdefaultpalette(self.mainframe)
109 bg, fg = self.colorList[self.colorIndex]
110 Pmw.Color.setscheme(self.mainframe, bg, foreground = fg)
111
112 self.pageCounter = self.pageCounter + 1
113 before = self.randomchoice(self.notebook.pagenames() + [Pmw.END])
114 pageName = 'page%d' % self.pageCounter
115 if self.pageCounter % 5 == 0:
116 tab_text = pageName + '\nline two'
117 else:
118 tab_text = pageName
119 classes = (None, Tkinter.Button, Tkinter.Label, Tkinter.Checkbutton)
120 cls = self.randomchoice((None,) + classes)
121 if cls is None:
122 print 'Adding', pageName, 'as a frame with a button'
123 if self.withTabs:
124 page = self.notebook.insert(pageName, before, tab_text = tab_text)
125 else:
126 page = self.notebook.insert(pageName, before)
127 button = Tkinter.Button(page,
128 text = 'This is button %d' % self.pageCounter)
129 button.pack(expand = 1)
130 else:
131 print 'Adding', pageName, 'using', cls
132 if self.withTabs:
133 page = self.notebook.insert(pageName, before,
134 tab_text = tab_text,
135 page_pyclass = cls,
136 page_text = 'This is a page using\na %s' % str(cls)
137 )
138 else:
139 page = self.notebook.insert(pageName, before,
140 page_pyclass = cls,
141 page_text = 'This is a page using\na %s' % str(cls)
142 )
143 if not self.withTabs:
144 self.optionmenu.setitems(
145 self.notebook.pagenames(), self.notebook.getcurselection())
146
147 apply(Pmw.Color.setscheme, (self.mainframe,), defaultPalette)
148
149 def addbutton(self):
150 # Add a button to a random page.
151
152 defaultPalette = Pmw.Color.getdefaultpalette(self.mainframe)
153 bg, fg = self.colorList[self.colorIndex]
154 Pmw.Color.setscheme(self.mainframe, bg, foreground = fg)
155
156 framePages = []
157 for pageName in self.notebook.pagenames():
158 page = self.notebook.page(pageName)
159 if page.__class__ == Tkinter.Frame:
160 framePages.append(pageName)
161
162 if len(framePages) == 0:
163 self.notebook.bell()
164 return
165
166 pageName = self.randomchoice(framePages)
167 print 'Adding extra button to', pageName
168 page = self.notebook.page(pageName)
169 button = Tkinter.Button(page, text = 'This is an extra button')
170 button.pack(expand = 1)
171
172 apply(Pmw.Color.setscheme, (self.mainframe,), defaultPalette)
173
174 def deletepage(self):
175 # Delete a random page
176
177 pageNames = self.notebook.pagenames()
178 if len(pageNames) == 0:
179 self.notebook.bell()
180 return
181
182 pageName = self.randomchoice(pageNames)
183 print 'Deleting', pageName
184 self.notebook.delete(pageName)
185 if not self.withTabs:
186 self.optionmenu.setitems(
187 self.notebook.pagenames(), self.notebook.getcurselection())
188
189 def changecolor(self):
190 self.colorIndex = self.colorIndex + 1
191 if self.colorIndex == len(self.colorList):
192 self.colorIndex = 0
193
194 bg, fg = self.colorList[self.colorIndex]
195 print 'Changing color to', bg
196 Pmw.Color.changecolor(self.mainframe, bg, foreground = fg)
197 self.notebook.recolorborders()
198
199 # Simple random number generator.
200 def randomchoice(self, selection):
201 num = len(selection)
202 self.rand = (self.rand * 125) % 2796203
203 index = self.rand % num
204 return selection[index]
205
206class PrintOne:
207 def __init__(self, text):
208 self.text = text
209
210 def __call__(self, text):
211 print self.text, text
212
213######################################################################
214
215# Create demo in root window for testing.
216if __name__ == '__main__':
217 root = Tkinter.Tk()
218 Pmw.initialise(root)
219 root.title(title)
220
221 widget = Demo(root)
222 exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
223 exitButton.pack()
224 root.mainloop()