/[ascend]/trunk/pygtk/loading.py
ViewVC logotype

Contents of /trunk/pygtk/loading.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2217 - (show annotations) (download) (as text)
Tue Jul 20 04:31:04 2010 UTC (9 years, 4 months ago) by jpye
File MIME type: text/x-python
File size: 4448 byte(s)
Remove print output when running with 'pythonw.exe' to avoid crash (see bug #471)
1 import sys
2 import config
3 import os.path
4
5 global have_gtk
6 have_gtk = False
7
8 if not sys.executable.endswith("pythonw.exe"):
9 print "PYTHON PATH =",sys.path
10
11 try:
12 import pygtk
13 pygtk.require('2.0')
14 import gtk
15 have_gtk = True
16 except Exception,e:
17
18 if sys.platform=="win32":
19 try:
20 from ctypes import c_int, WINFUNCTYPE, windll
21 from ctypes.wintypes import HWND, LPCSTR, UINT
22 prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
23 paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
24 MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)
25 MessageBox(text="""ASCEND could not load PyGTK. Probably this is because
26 either PyGTK, PyCairo, PyGObject or GTK+ are not installed on your
27 system. Please try re-installing ASCEND to rectify the problem.""")
28 except:
29 pass
30 else:
31 print "PyGTK COULD NOT BE LOADED (is it installed? do you have X-Windows running?) (%s)" % str(e)
32
33 sys.exit("FATAL ERROR: PyGTK not available, unable to start ASCEND.")
34
35 global _messages
36 _messages = []
37
38 def get_messages():
39 return _messages
40
41 def load_matplotlib(throw=False,alert=False):
42 print_status("Loading python matplotlib")
43 try:
44 import matplotlib
45 matplotlib.use('GTKAgg')
46
47 try:
48 print_status("Trying python numpy")
49 import numpy
50 matplotlib.rcParams['numerix'] = 'numpy'
51 print_status("","Using python module numpy")
52 except ImportError:
53 try:
54 print_status("Trying python numarray")
55 import numarray
56 matplotlib.rcParams['numerix'] = 'numarray'
57 print_status("","Using python module numarray")
58 except ImportError:
59 try:
60 print_status("Trying python Numeric")
61 import Numeric
62 matplotlib.rcParams['numerix'] = 'Numeric'
63 print_status("","Using python module Numeric")
64 except ImportError:
65 print_status("","FAILED TO LOAD A NUMERIC MODULE FOR PYTHON")
66 import pylab
67
68
69 except ImportError,e:
70 print_status("","FAILED TO LOAD MATPLOTLIB")
71 if alert or throw:
72 _d = gtk.MessageDialog(None,gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT
73 ,gtk.MESSAGE_ERROR,gtk.BUTTONS_CLOSE,"Plotting functions are not available unless you have 'matplotlib' installed.\n\nSee http://matplotlib.sf.net/\n\nFailed to load matplotlib (%s)" % str(e)
74 )
75 _d.run()
76 _d.destroy()
77 while gtk.events_pending():
78 gtk.main_iteration(False)
79 if throw:
80 raise RuntimeError("Failed to load plotting library 'matplotlib'. (%s)" % str(e))
81
82
83 class LoadingWindow:
84 def __init__(self):
85 self.is_loading = False
86 self.set_assets_dir(config.PYGTK_ASSETS)
87
88 def set_assets_dir(self, d):
89 self.assetsdir = d
90 self.splashfile = os.path.join(self.assetsdir,'ascend-loading.png')
91
92 def create_window(self):
93 if have_gtk:
94 if os.path.exists(self.splashfile):
95 _w = gtk.Window(gtk.WINDOW_TOPLEVEL)
96 _w.set_decorated(False)
97 _w.set_position(gtk.WIN_POS_CENTER)
98 _a = gtk.Alignment()
99 _a.set_padding(4,4,4,4)
100 _w.add(_a)
101 _a.show()
102 _v = gtk.VBox()
103 _a.add(_v)
104 _v.show()
105 _i = gtk.Image()
106 self.image = _i
107 _i.set_pixel_size(3)
108 _i.set_from_file(self.splashfile)
109 _v.add(_i)
110 _i.show()
111 _l = gtk.Label("Loading ASCEND...")
112 _l.set_justify(gtk.JUSTIFY_CENTER)
113 _v.add(_l)
114 _l.show()
115 _w.show()
116 self.window = _w
117 self.label = _l
118 self.is_loading = True
119 while gtk.events_pending():
120 gtk.main_iteration(False)
121 else:
122 pass
123 #do nothing, don't know where splash file is yet
124 else:
125 print "DON'T HAVE GTK!"
126 sys.exit(1)
127
128 def print_status(self,status,msg=None):
129 if self.is_loading:
130 if not sys.executable.endswith("pythonw.exe"):
131 print status
132 self.label.set_text(status)
133 if msg is not None:
134 try:
135 sys.stderr.write(msg+"\n")
136 except IOError:
137 pass
138 _messages.append(msg)
139 while gtk.events_pending():
140 gtk.main_iteration(False)
141 else:
142 try:
143 sys.stderr.write("\r \r")
144 if msg!=None:
145 sys.stderr.write(msg+"\n")
146 _messages.append(msg)
147 sys.stderr.write(status+"...\r")
148 sys.stderr.flush()
149 except IOError:
150 pass
151
152 def complete(self):
153 if self.is_loading:
154 self.window.destroy()
155 self.is_loading = False
156
157 global w
158
159 def print_status(status,msg=None):
160 w.print_status(status,msg)
161
162 def complete():
163 w.complete()
164
165 def create_window(assetsdir=config.PYGTK_ASSETS):
166 w.set_assets_dir(assetsdir)
167 w.create_window()
168
169 w = LoadingWindow()
170 create_window()

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22