FileChooser.py 837 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2022 Stephen Stengel <stephen.stengel@cwu.edu> and friends
  5. #
  6. import gi
  7. gi.require_version("Gtk", "3.0")
  8. from gi.repository import Gtk
  9. class FileChooser():
  10. def __init__(self, titleText):
  11. self.pathStr = None #Stores the returned path.
  12. self.titleText = titleText
  13. #Sets up a file chooser dialog window object.
  14. dialog = Gtk.FileChooserDialog( \
  15. self.titleText, \
  16. None, \
  17. Gtk.FileChooserAction.SELECT_FOLDER, \
  18. (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
  19. #Opens the dialog, waits for the return value.
  20. response = dialog.run()
  21. #Only save the path if the user clicked OK and not cancel.
  22. if response == Gtk.ResponseType.OK:
  23. self.pathStr = dialog.get_filename()
  24. #Close the dialog.
  25. dialog.destroy()