Package pyraf :: Module splash
[hide private]
[frames] | no frames]

Source Code for Module pyraf.splash

  1  """splash.py: Display PyRAF splash screen 
  2   
  3  $Id: splash.py 1463 2011-06-24 22:58:30Z stsci_embray $ 
  4   
  5  R. White, 2001 Dec 15 
  6  """ 
  7   
  8  from __future__ import division # confidence high 
  9   
 10  import os, sys, Tkinter 
 11  from stsci.tools.irafglobals import IrafPkg 
 12  import wutil 
 13   
 14  logo = "pyraflogo_rgb_web.gif" 
 15   
16 -class SplashScreen(Tkinter.Toplevel):
17 18 """Base class for splash screen 19 20 Subclass and override createWidgets(). 21 In constructor of main window/application call 22 - S = SplashScreen(main=self) (if caller is Toplevel) 23 - S = SplashScreen(main=self.master) (if caller is Frame) 24 - S.Destroy() after you are done creating your widgets etc. 25 26 Based closely on news posting by Alexander Schliep, 07 Apr 1999 27 """ 28
29 - def __init__(self, master=None, borderwidth=4, relief=Tkinter.RAISED, **kw):
30 Tkinter.Toplevel.__init__(self, master, relief=relief, 31 borderwidth=borderwidth, **kw) 32 if self.master.master != None: # Why? 33 self.master.master.withdraw() 34 self.master.withdraw() 35 self.overrideredirect(1) 36 self.createWidgets() 37 self.after_idle(self.centerOnScreen) 38 self.update()
39
40 - def centerOnScreen(self):
41 self.update_idletasks() 42 xmax = self.winfo_screenwidth() 43 ymax = self.winfo_screenheight() 44 x0 = (xmax - self.winfo_reqwidth()) // 2 45 y0 = (ymax - self.winfo_reqheight()) // 2 46 self.geometry("+%d+%d" % (x0, y0))
47
48 - def createWidgets(self):
49 # Implement in derived class 50 pass
51
52 - def Destroy(self):
53 self.master.update() 54 self.master.deiconify() 55 self.withdraw()
56 57
58 -class PyrafSplash(SplashScreen):
59 60 """PyRAF splash screen 61 62 Contains an image and one or more text lines underneath. The number 63 of lines is determined by the value of the text argument, which may be 64 a string (for a single line or, with embedded newlines, multiple lines) 65 or a list of strings. The text line(s) can be changed using the write() 66 method. 67 """ 68
69 - def __init__(self, filename=logo, text=None, textcolor="blue", **kw):
70 # look for file in both local directory and this script's directory 71 if not os.path.exists(filename): 72 tfilename = os.path.join(os.path.dirname(__file__),filename) 73 if not os.path.exists(tfilename): 74 raise ValueError("Splash image `%s' not found" % filename) 75 filename = tfilename 76 self.filename = filename 77 self.nlines = 1 78 self.textcolor = textcolor 79 if text: 80 if isinstance(text, type("")): 81 text = text.split("\n") 82 self.nlines = len(text) 83 self.initialText = text 84 else: 85 self.initialText = [None] 86 # put focus on this app (Mac only) 87 self.__termWin = None 88 if wutil.hasGraphics and wutil.WUTIL_ON_MAC: 89 self.__termWin = wutil.getFocalWindowID() # the terminal window 90 wutil.forceFocusToNewWindow() 91 # create it 92 SplashScreen.__init__(self, **kw) 93 self.defaultCursor = self['cursor'] 94 self.bind("<Button>", self.killCursor) 95 self.bind("<ButtonRelease>", self.Destroy)
96
97 - def createWidgets(self):
98 """Create pyraf splash image""" 99 self.img = Tkinter.PhotoImage(file=self.filename) 100 width = self.img.width()+20 101 iheight = self.img.height() 102 height = iheight+10+15*self.nlines 103 self.canvas = Tkinter.Canvas(self, width=width, height=height, 104 background=self["background"]) 105 self.image = self.canvas.create_image(width//2, 5+iheight//2, image=self.img) 106 self.text = self.nlines*[None] 107 minx = 0 108 font = ("helvetica", 12) 109 for i in range(self.nlines): 110 y = height-(self.nlines-i)*15+8 111 tval = self.initialText[i] or "" 112 self.text[i] = self.canvas.create_text(width//2, y, 113 text=tval, fill=self.textcolor, font=font) 114 minx = min(minx, self.canvas.bbox(self.text[i])[0]) 115 if minx<3: 116 # expand window and recenter all items 117 width = width+(3-minx)*2 118 self.canvas.configure(width=width) 119 self.canvas.coords(self.image, width//2, 5+iheight//2) 120 for i in range(self.nlines): 121 y = height-(self.nlines-i)*15+8 122 self.canvas.coords(self.text[i], width//2, y) 123 self.canvas.pack()
124
125 - def write(self, s):
126 """Set text string""" 127 if self.text is None: 128 return 129 if isinstance(s, type('')): 130 s = s.split("\n") 131 s = s[:len(self.text)] 132 for i in range(len(s)): 133 if s[i] is not None: 134 self.canvas.itemconfigure(self.text[i], text=s[i]) 135 self.update_idletasks()
136
137 - def killCursor(self, event=None):
138 """Set 'pirate' kill cursor on button down""" 139 self['cursor'] = 'pirate'
140
141 - def Destroy(self, event=None):
142 if event: 143 # make sure button release occurred in window 144 # Tkinter should take care of this but doesn't 145 if event.x<0 or event.x>=self.winfo_width() or \ 146 event.y<0 or event.y>=self.winfo_height(): 147 self['cursor'] = self.defaultCursor 148 return 149 self.destroy() 150 # disable future writes 151 self.text = None 152 self.update_idletasks() 153 # put focus back on terminal (if set) 154 if self.__termWin: 155 wutil.setFocusTo(self.__termWin)
156
157 -class IrafMonitorSplash(PyrafSplash):
158 159 """PyRAF splash screen that also acts as IRAF task execution monitor 160 161 Usually start this by calling the splash() function in this module. 162 """ 163
164 - def __init__(self, label="PyRAF Execution Monitor", **kw):
165 PyrafSplash.__init__(self, text=[None, label], **kw) 166 # self.stack tracks messages displayed in monitor 167 self.stack = [] 168 import iraftask 169 iraftask.executionMonitor = self.monitor
170
171 - def monitor(self, task=None):
172 if task is None: 173 # if arg is omitted, restore monitor message to previous value 174 try: 175 self.stack.pop() 176 msg = self.stack[-1] 177 except IndexError: 178 msg = "" 179 else: 180 name = task.getName() 181 if name != 'cl': 182 if isinstance(task, IrafPkg): 183 msg = "Loading %s" % name 184 else: 185 msg = "Running %s" % name 186 else: 187 # cl task message includes input file name 188 try: 189 msg = "cl %s" % os.path.basename(sys.stdin.name) 190 except AttributeError: 191 msg = "cl <pipe>" 192 self.stack.append(msg) 193 self.write(msg)
194
195 - def Destroy(self, event=None):
196 """Shut down window and disable monitor""" 197 import iraftask 198 if iraftask.executionMonitor == self.monitor: 199 iraftask.executionMonitor = None 200 PyrafSplash.Destroy(self, event)
201
202 -def splash(label="PyRAF Execution Monitor", background="LightYellow", **kw):
203 """Display the PyRAF splash screen 204 205 Silently does nothing if Tkinter is not usable. 206 """ 207 if wutil.hasGraphics: 208 try: 209 return IrafMonitorSplash(label, background=background, **kw) 210 except Tkinter.TclError: 211 pass 212 return None
213 214 215 if __name__ == "__main__": 216 import time 217 s = PyrafSplash() 218 print "Sleeping 2 seconds..." 219 time.sleep(2) 220