3 # ------------------------------------------------------------------
4 # Display a splash screen as quickly as possible (before importing
5 # modules and initialising Pmw).
8 root = Tkinter.Tk(className = 'Demo')
11 splash = Tkinter.Toplevel()
13 splash.title('Welcome to the Pmw demos')
14 text = Tkinter.Label(splash,
15 font=('Helvetica', 16, 'bold'),
20 'Welcome to the Pmw megawidgets demo.\n'
22 'In a moment the main window will appear.\n'
23 'Please enjoy yourself while you wait.\n'
24 'You may be interested to know that splash screens\n'
25 '(as this window is called) were first devised to draw\n'
26 'attention away from the fact the certain applications\n'
27 'are slow to start. They are normally flashier and more\n'
28 'entertaining than this one. This is a budget model.'
30 text.pack(fill = 'both', expand = 1)
31 splash.update_idletasks()
33 width = splash.winfo_reqwidth()
34 height = splash.winfo_reqheight()
35 x = (root.winfo_screenwidth() - width) / 2 - root.winfo_vrootx()
36 y = (root.winfo_screenheight() - height) / 3 - root.winfo_vrooty()
41 geometry = '%dx%d+%d+%d' % (width, height, x, y)
43 splash.geometry(geometry)
44 splash.update_idletasks()
48 # ------------------------------------------------------------------
50 # Now crank up the application windows.
62 # Find where the other scripts are, so they can be listed.
63 if __name__ == '__main__':
64 script_name = sys.argv[0]
66 script_name = imp.find_module('DemoVersion')[1]
68 script_name = os.path.normpath(script_name)
69 script_name = DemoVersion.expandLinks(script_name)
70 script_dir = os.path.dirname(script_name)
71 script_dir = DemoVersion.expandLinks(script_dir)
73 # Add the '../../..' directory to the path.
74 package_dir = os.path.dirname(script_dir)
75 package_dir = DemoVersion.expandLinks(package_dir)
76 package_dir = os.path.dirname(package_dir)
77 package_dir = DemoVersion.expandLinks(package_dir)
78 package_dir = os.path.dirname(package_dir)
79 package_dir = DemoVersion.expandLinks(package_dir)
80 sys.path[:0] = [package_dir]
82 # Import Pmw after modifying sys.path (it may not be in the default path).
84 DemoVersion.setPmwVersion()
86 class Demo(Pmw.MegaWidget):
88 def __init__(self, parent=None, **kw):
90 # Define the megawidget options.
92 self.defineoptions(kw, optiondefs)
94 # Initialise the base class (after defining the options).
95 Pmw.MegaWidget.__init__(self, parent)
97 # Create the contents.
100 panes = Pmw.PanedWidget(top, orient = 'horizontal')
101 panes.pack(fill = 'both', expand = 1)
103 panes.add('widgetlist')
104 self._widgetlist = Pmw.ScrolledListBox(panes.pane('widgetlist'),
105 selectioncommand = Pmw.busycallback(self.startDemo),
106 label_text = 'Select a widget:',
108 vscrollmode = 'dynamic',
109 hscrollmode = 'none',
110 listbox_exportselection = 0)
111 self._widgetlist.pack(fill = 'both', expand = 1, padx = 8)
114 self._status = Tkinter.Label(panes.pane('info'))
115 self._status.pack(padx = 8, anchor = 'w')
117 self._example = Tkinter.Frame(panes.pane('info'),
120 background = 'white')
121 self._example.pack(fill = 'both', expand = 1, padx = 8)
123 self.buttonBox = Pmw.ButtonBox(top)
124 self.buttonBox.pack(fill = 'x')
126 # Add the buttons and make them all the same width.
127 self._traceText = 'Trace tk calls'
128 self._stopTraceText = 'Stop trace'
129 self.buttonBox.add('Trace', text = self._traceText,
130 command = self.trace)
131 self.buttonBox.add('Code', text = 'Show code', command = self.showCode)
132 self.buttonBox.add('Exit', text = 'Exit', command = sys.exit)
133 self.buttonBox.alignbuttons()
135 # Create the window to display the python code.
136 self.codeWindow = Pmw.TextDialog(parent,
137 title = 'Python source',
138 buttons = ('Dismiss',),
139 scrolledtext_labelpos = 'n',
140 label_text = 'Source')
141 self.codeWindow.withdraw()
142 self.codeWindow.insert('end', '')
147 # Check keywords and initialise options.
148 self.initialiseoptions()
151 # Import the selected module and create and instance of the module's
154 sels = self._widgetlist.getcurselection()
156 print 'No demonstrations to display'
160 # Ignore if this if it is a sub title.
161 if demoName[0] != ' ':
162 self._widgetlist.bell()
165 # Strip the leading two spaces.
166 demoName = demoName[2:]
168 # Ignore if this demo is already being shown.
169 if self.demoName == demoName:
172 self.demoName = demoName
174 self.showStatus('Loading ' + demoName)
176 self.update_idletasks()
178 for window in self._example.winfo_children():
181 frame = Tkinter.Frame(self._example)
182 frame.pack(expand = 1)
183 exec 'import ' + demoName
184 # Need to keep a reference to the widget, so that variables, etc
186 self.widget = eval(demoName + '.Demo(frame)')
187 title = eval(demoName + '.title')
188 self.showStatus(title)
190 if self.codeWindow.state() == 'normal':
193 def showStatus(self, text):
194 self._status.configure(text = text)
197 if self.codeWindow.state() != 'normal':
198 if self.demoName is None:
199 print 'No demonstration selected'
203 self.codeWindow.show()
205 def insertCode(self):
206 self.codeWindow.clear()
207 fileName = os.path.join(script_dir, self.demoName + '.py')
208 self.codeWindow.importfile(fileName)
209 self.codeWindow.configure(label_text = self.demoName + ' source')
212 text = self.buttonBox.component('Trace').cget('text')
213 if text == self._traceText:
214 self.buttonBox.configure(Trace_text = self._stopTraceText)
216 self.showStatus('Trace will appear on standard output')
218 self.buttonBox.configure(Trace_text = self._traceText)
220 self.showStatus('Tk call tracing stopped')
222 def _loadDemos(self):
223 files = os.listdir(script_dir)
228 if re.search('.py$', file) is not None and \
229 file not in ['All.py', 'DemoVersion.py', 'Args.py']:
231 index = string.find(demoName, '_')
235 testattr = demoName[:index]
236 if hasattr(Pmw, testattr):
237 megawidgets.append(demoName)
239 others.append(demoName)
241 self._widgetlist.insert('end', 'Megawidget demos:')
242 for name in megawidgets:
243 self._widgetlist.insert('end', ' ' + name)
244 self._widgetlist.insert('end', 'Other demos:')
246 self._widgetlist.insert('end', ' ' + name)
247 self._widgetlist.select_set(1)
250 def __init__(self, displayCommand):
251 self.displayCommand = displayCommand
254 def write(self, text):
255 if self.text[-1] == '\n':
258 self.text = self.text + text
259 if self.text[-1] == '\n':
260 text = self.text[:-1]
263 self.displayCommand(text)
270 commandLineArgSpecs = (
271 ('fontscheme', 0, 'scheme', 'fonts to use [eg pmw2] (Tk defaults)'),
272 ('fontsize', 0, 'num', 'size of fonts to use with fontscheme', defaultFontSize),
273 ('stdout', 0, Args.Bool, 'print messages rather than display in label'),
277 msg = Args.parseArgs(program, sys.argv, commandLineArgSpecs, 0)
282 size = Args.get('fontsize')
283 fontScheme = Args.get('fontscheme')
284 Pmw.initialise(root, size = size, fontScheme = fontScheme, useTkOptionDb = 1)
286 root.title('Pmw ' + Pmw.version() + ' megawidget demonstration')
290 geometry = '1000x700'
291 root.geometry(geometry)
294 demo.pack(fill = 'both', expand = 1)
297 # Redirect standard output from demos to status line (unless -stdout
298 # option given on command line).
299 if not Args.get('stdout'):
300 sys.stdout = StdOut(demo.showStatus)
302 # Start the first demo.
305 # Get rid of the splash screen