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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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