FWIW, one small step further: here is a short script that will show the
Windows dialog on windows if possible, and fall back to the PyGTK one
where necessary (on Linux or on Windows without win32all). See attached.

It's only designed for opening files at this stage, but perhaps it can
be taken further.

Cheers
JP

John Pye wrote:
> I've posted a working version of this direct from the demo scripts in
> the win32all package.
>
> See http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq21.013.htp
>
> Cheers
> JP
>
> Nikos Kouremenos wrote:
>   
>> Chris, is it possible you could put a comment or two and add it to the FAQ?
>>
>> thanks in advance
>>     
-- 
John Pye
School of Mechanical and Manufacturing Engineering
The University of New South Wales
Sydney  NSW 2052  Australia
t +61 2 9385 5127
f +61 2 9663 1222
mailto:john.pye_AT_student_DOT_unsw.edu.au
http://pye.dyndns.org/

import platform, os
import gtk

class WinFileFilter:
        def __init__(self,name,patternarray):
                self.name = name + " (" + ", ".join(patternarray) + ")"
                self.pattern = ";".join(patternarray)
        def __repr__(self):
                return '%s\0%s' % (self.name, self.pattern)

class FileChooser:
        def __init__(self):
                self.ext = {}
                self.filters = []
                if platform.system()=="Windows":
                        try:
                                import win32gui
                                self.iswin = True
                                return
                        except ImportError:
                                pass
                self.iswin = False
                self.chooser = gtk.FileChooserDialog()
                self.chooser.add_buttons(gtk.STOCK_OPEN,42)

        
        def add_filter(self,name,patternarray):
                if self.iswin:
                        self.filters.append(WinFileFilter(name,patternarray))
                else:
                        _f = gtk.FileFilter()
                        _f.set_name(name)
                        for _p in patternarray:
                                _f.add_pattern(_p)
                        self.chooser.add_filter(_f)
        
        def do(self):     
                if self.iswin:
                        return self.do_windows()
                else:
                        return self.do_gtk()

        def do_windows(self):
                import win32gui, win32con
                _fa = []
                for _f in self.filters:
                        _fa.append(repr(_f))
                filter='\0'.join(_fa)+'\0'
                customfilter='Other files (*.*)\0*.*\0'
                print "FILTER:",repr(filter)
                
                fname = "ERROR"
                
                try:
                        fname,customfilter,flags = win32gui.GetOpenFileNameW(
                                InitialDir=os.getcwd(),
                                Flags=win32con.OFN_EXPLORER,
                                File='', DefExt='py',
                                Title='Open File...',
                                Filter=filter,
                                CustomFilter=customfilter,
                                FilterIndex=1
                        )
                except Exception, e:
                        if hasattr(e,'what'):
                                print e.what()
                        raise RuntimeError("File select error!")
                
                return fname

        def do_gtk(self):
                print "LAUNCHING..."
                self.add_filter("Other files",["*.*"])
                self.chooser.run()
                print "DONE..."
                return self.chooser.get_filename()
                

f = FileChooser()
f.add_filter("ASCEND files",["*.a4c","*.a4l"])
print "SELECTED FILE",f.do()
_______________________________________________
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to