WxRuby

Restez toujours informé : suivez-nous sur Google (☆)

Introduction

WxRuby est un toolkit graphique libre utilisant le langage de programmation Ruby. Il est basé sur le widget toolkit multiplate-forme wxWidgets, écrit en C++.

wxRuby permet d'utiliser Ruby pour la création d'applications graphiques sur Windows, OS X, Linux GTK, etc. Les applications ainsi créées conservent l'apparence native du système sur lequel elles sont exécutées.

Licence

WxRuby est publié sous la licence WxRuby2.

Exemple

  1. #!/usr/bin/env ruby

  2. begin

  3. require 'rubygems'

  4. rescue LoadError

  5. end

  6. require 'wx'

  7. A Wx::Frame is a self-contained, top-level Window that can contain
  8. controls, menubars, and statusbars
  9. class MinimalFrame < Wx::Frame

  10. def initialize(title)

  11. The main application frame has no parent (nil)
  12. super(nil, :title => title, :size => [ 400, 300 ])

  13. menu_bar = Wx::MenuBar.new

  14. The "file" menu
  15. menu_file = Wx::Menu.new

  16. Using Wx::ID_EXIT standard id means the menu item will be given
  17. the right label for the platform and language, and placed in the
  18. correct platform-specific menu - eg on OS X, in the Application's menu
  19. menu_file.append(Wx::ID_EXIT, "E&xit\tAlt-X", "Quit this program")

  20. menu_bar.append(menu_file, "&File")

  21. The "help" menu
  22. menu_help = Wx::Menu.new

  23. menu_help.append(Wx::ID_ABOUT, "&About...\tF1", "Show about dialog")

  24. menu_bar.append(menu_help, "&Help")

  25. Assign the menubar to this frame
  26. self.menu_bar = menu_bar

  27. Create a status bar at the bottom of the frame
  28. create_status_bar(2)

  29. self.status_text = "Welcome to wxRuby!"

  30. Set it up to handle menu events using the relevant methods.
  31. evt_menu Wx::ID_EXIT, :on_quit

  32. evt_menu Wx::ID_ABOUT, :on_about

  33. end

  34. End the application; it should finish automatically when the last
  35. window is closed.
  36. def on_quit

  37. close()

  38. end

  39. show an 'About' dialog - WxRuby's about_box function will show a
  40. platform-native 'About' dialog, but you could also use an ordinary
  41. Wx::MessageDialog here.
  42. def on_about

  43. Wx::about_box(:name => self.title,

  44. :version => Wx::WXRUBY_VERSION,

  45. :description => "This is the minimal sample",

  46. :developers => ['The wxRuby Development Team'] )

  47. end

  48. end

  49. Wx::App is the container class for any wxruby app. To start an
  50. application, either define a subclass of Wx::App, create an instance,
  51. and call its main_loop method, OR, simply call the Wx::App.run class
  52. method, as shown here.
  53. Wx::App.run do

  54. self.app_name = 'Minimal'

  55. frame = MinimalFrame.new("Minimal wxRuby App")

  56. frame.show

  57. end