g.gui.example.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: g.gui.iclass
  5. # AUTHOR(S): Anna Petrasova
  6. # PURPOSE: Example GUI application
  7. # COPYRIGHT: (C) 2012-2014 by the GRASS Development Team
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. ############################################################################
  20. #%module
  21. #% description: Example GUI application which displays raster map and further information
  22. #% keyword: example
  23. #% keyword: GUI
  24. #% keyword: raster
  25. #%end
  26. #%option G_OPT_R_INPUT
  27. #% description: Name of raster map to load
  28. #% required: no
  29. #%end
  30. import os
  31. import sys
  32. import wx
  33. # i18n is taken care of in the grass library code.
  34. # So we need to import it before any of the GUI code.
  35. import grass.script.core as gcore
  36. if __name__ == '__main__':
  37. wxbase = os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython')
  38. if wxbase not in sys.path:
  39. sys.path.append(wxbase)
  40. from core.globalvar import CheckWxVersion
  41. from core.giface import StandaloneGrassInterface
  42. from core.utils import GuiModuleMain
  43. from core.settings import UserSettings
  44. from example.frame import ExampleMapFrame
  45. def main():
  46. options, flags = gcore.parser()
  47. if options['input']:
  48. map_name = gcore.find_file(name=options['input'], element='cell')['fullname']
  49. if not map_name:
  50. gcore.fatal(_("Raster map <{raster}> not found").format(raster=options['input']))
  51. # define display driver (avoid 'no graphics device selected' error at start up)
  52. driver = UserSettings.Get(group='display', key='driver', subkey='type')
  53. if driver == 'png':
  54. os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
  55. else:
  56. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  57. # launch application
  58. app = wx.App()
  59. if not CheckWxVersion([2, 9]):
  60. wx.InitAllImageHandlers()
  61. # show main frame
  62. giface = StandaloneGrassInterface()
  63. frame = ExampleMapFrame(parent=None, giface=giface)
  64. if options['input']:
  65. giface.WriteLog(_("Loading raster map <{raster}>...").format(raster=map_name))
  66. frame.SetLayer(map_name)
  67. frame.Show()
  68. app.MainLoop()
  69. if __name__ == '__main__':
  70. GuiModuleMain(main)