/[ascend]/trunk/pygtk/interface/filechooser.py
ViewVC logotype

Contents of /trunk/pygtk/interface/filechooser.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 365 - (show annotations) (download) (as text)
Thu Mar 9 06:20:17 2006 UTC (14 years, 4 months ago) by johnpye
File MIME type: text/x-python
File size: 1716 byte(s)
Adding a windows/gtk adaptable filechooser.
1 import platform, os
2 import gtk
3
4 class WinFileFilter:
5 def __init__(self,name,patternarray):
6 self.name = name + " (" + ", ".join(patternarray) + ")"
7 self.pattern = ";".join(patternarray)
8 def __repr__(self):
9 return '%s\0%s' % (self.name, self.pattern)
10
11 class FileChooser:
12 def __init__(self):
13 self.ext = {}
14 self.filters = []
15 if platform.system()=="Windows":
16 try:
17 import win32gui
18 self.iswin = True
19 return
20 except ImportError:
21 pass
22 self.iswin = False
23 self.chooser = gtk.FileChooserDialog()
24
25
26 def add_filter(self,name,patternarray):
27 if self.iswin:
28 self.filters.append(WinFileFilter(name,patternarray))
29 else:
30 _f = gtk.FileFilter()
31 _f.set_name(name)
32 for _p in patternarray:
33 _f.add_pattern(_p)
34 self.chooser.add_filter(_f)
35
36 def do(self):
37 if self.iswin:
38 return self.do_windows()
39 else:
40 return self.do_gtk()
41
42 def do_windows(self):
43 import win32gui, win32con
44 _fa = []
45 for _f in self.filters:
46 _fa.append(repr(_f))
47 filter='\0'.join(_fa)+'\0'
48 customfilter='Other files (*.*)\0*.*\0'
49 print "FILTER:",repr(filter)
50
51 fname = "ERROR"
52
53 try:
54 fname,customfilter,flags = win32gui.GetOpenFileNameW(
55 InitialDir=os.getcwd(),
56 Flags=win32con.OFN_EXPLORER,
57 File='', DefExt='py',
58 Title='Open File...',
59 Filter=filter,
60 CustomFilter=customfilter,
61 FilterIndex=1
62 )
63 except Exception, e:
64 if hasattr(e,'what'):
65 print e.what()
66 raise RuntimeError("File select error!")
67
68 return fname
69
70 def do_gtk(self):
71 import gtk;
72
73
74 f = FileChooser()
75 f.add_filter("ASCEND files",["*.a4c","*.a4l"])
76 print "SELECTED FILE",f.do()

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