Fredrik Lundh | 4ecd713 | 2001-10-29 22:58:55 +0000 | [diff] [blame] | 1 | # |
| 2 | # tkDirectoryChooser.py |
| 3 | # $Id$ |
| 4 | # |
| 5 | # tk common directory dialogue |
| 6 | # |
| 7 | # this module provides interfaces to the native directory dialogue |
| 8 | # available in Tk 8.3 and newer. |
| 9 | # |
| 10 | # written by Fredrik Lundh, November 2000. |
| 11 | # |
| 12 | |
| 13 | # |
| 14 | # options (all have default values): |
| 15 | # |
| 16 | # - initialdir: initial directory. preserved by dialog instance. |
| 17 | # |
| 18 | # - mustexist: if true, user must pick an existing directory |
| 19 | # |
| 20 | # - parent: which window to place the dialog on top of |
| 21 | # |
| 22 | # - title: dialog title |
| 23 | # |
| 24 | |
| 25 | from tkCommonDialog import Dialog |
| 26 | |
| 27 | class Chooser(Dialog): |
| 28 | |
| 29 | command = "tk_chooseDirectory" |
| 30 | |
| 31 | def _fixresult(self, widget, result): |
| 32 | if result: |
| 33 | # keep directory until next time |
| 34 | self.options["initialdir"] = result |
| 35 | self.directory = result # compatibility |
| 36 | return result |
| 37 | |
| 38 | # |
| 39 | # convenience stuff |
| 40 | |
| 41 | def askdirectory(**options): |
| 42 | "Ask for a directory name" |
| 43 | |
| 44 | return apply(Chooser, (), options).show() |
| 45 | |
| 46 | # -------------------------------------------------------------------- |
| 47 | # test stuff |
| 48 | |
| 49 | if __name__ == "__main__": |
| 50 | |
| 51 | print "directory", askdirectory() |
| 52 | |