g.gui.example.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python
  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. #% keywords: example
  23. #% keywords: GUI
  24. #% keywords: 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. import grass.script.core as gcore
  34. if __name__ == '__main__':
  35. wxbase = os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython')
  36. if wxbase not in sys.path:
  37. sys.path.append(wxbase)
  38. from core.globalvar import CheckWxVersion
  39. from core.giface import StandaloneGrassInterface
  40. from core.utils import _, GuiModuleMain
  41. from core.settings import UserSettings
  42. from example.frame import ExampleMapFrame
  43. def main():
  44. options, flags = gcore.parser()
  45. if options['input']:
  46. map_name = gcore.find_file(name=options['input'], element='cell')['fullname']
  47. if not map_name:
  48. gcore.fatal(_("Raster map <{raster}> not found").format(raster=options['input']))
  49. # define display driver (avoid 'no graphics device selected' error at start up)
  50. driver = UserSettings.Get(group='display', key='driver', subkey='type')
  51. if driver == 'png':
  52. os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
  53. else:
  54. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  55. # launch application
  56. app = wx.App()
  57. if not CheckWxVersion([2, 9]):
  58. wx.InitAllImageHandlers()
  59. # show main frame
  60. giface = StandaloneGrassInterface()
  61. frame = ExampleMapFrame(parent=None, giface=giface)
  62. if options['input']:
  63. giface.WriteLog(_("Loading raster map <{raster}>...").format(raster=map_name))
  64. frame.SetLayer(map_name)
  65. frame.Show()
  66. app.MainLoop()
  67. if __name__ == '__main__':
  68. GuiModuleMain(main)