#!/usr/bin/env python # #copied from $Id: OptionColorButton.py,v 1.6 2001/11/03 11:05:22 doughellmann Exp $ """Button which controls a file chooser dialog. This button type is intended to be used in a preferences dialog. The 'PrefsDialog' and 'TagAttributeChooserDialog' use this. """ # # Import system modules # import Pmw from Tkinter import * from tkFileDialog import askopenfilename # # Import Local modules # # # Module # class OptionFileChooserButton(Pmw.LabeledWidget): """Button to use in option dialog to let the user choose a File. Options 'variable' -- The Tkinter StringVar instance which should be keep up to date with the option value. """ def __init__(self, parent=None, **kw): optiondefs = ( ('variable', None, Pmw.INITOPT), ('command', None, Pmw.INITOPT), ) self.internal_variable = StringVar() self.internal_variable.trace('w', self.variable_trace) self.defineoptions(kw, optiondefs) Pmw.LabeledWidget.__init__(self, parent=parent) interior = self.interior() l = self.createcomponent( 'label1', (), None, Label, (interior,), text=' ', relief='raised', ) l.pack( side=LEFT, fill=X, ) w = self.createcomponent( 'button', (), None, Button, (interior,), text=u'Pick\u2026', command=self.command, ) w.pack( side=LEFT, expand=YES, fill=X, ) w.grid_columnconfigure( 1, weight=1 ) self.initialiseoptions(self.__class__) if self['variable']: self.internal_variable.set(self['variable'].get()) return def variable_trace(self, *args): "Follow the variable changes as associated with user actions." #print 'tracing ', args current_val = self.internal_variable.get() self.configure(label1_text=current_val) if self['variable']: self['variable'].set(current_val) return def command(self, *args): "Callback called when user clicks on button." default = self.internal_variable.get() fn = askopenfilename( filetypes=[('All Types','*'),('vcf','*.vcf')], initialfile=default) #print 'filanem is ', fn if fn: #@@make sure readable? self.internal_variable.set(fn) if self['command']: self['command']() return def get(self): "Return the file name." return self.internal_variable.get() def set(self, newVal): "Set the file name." self.internal_variable.set(newVal) return if __name__=='__main__': root = Tk() btn = OptionFileChooserButton(root) btn.pack() root.mainloop()