Next Previous Contents

3. Grafisk brukergrensesnitt

Masse valgmuligheter. Tk, FOX, wxWidgets, Qt, GTK, Motif, FLTK og stort sett de fleste andre GUI-bibliotekene har Ruby-bindinger.

3.1 FOX

FXRuby er en utvidelse for å kunne bruke det grafiske brukergrensesnittet FOX i Ruby.

  1| require 'fox'
  2| 
  3| class Verden
  4|   include Fox
  5| 
  6|   def initialize
  7|     @app = FXApp.new
  8|     @main = FXMainWindow.new(@app, "Hei verden!") 
  9|     
 10|     @hadet_knapp = 
 11|       FXButton.new(@main, "&Ha det bra!", 
 12|                    nil, @app,
 13|                    FXApp::ID_QUIT )
 14| 
 15|     @hei_knapp = FXButton.new(@main, "Si hei!")
 16|     @hei_knapp.connect(SEL_COMMAND) do
 17|       puts "Hei verden!"
 18|     end      
 19|   end
 20| 
 21|   def start
 22|     @app.create
 23|     @main.show  # Vis vinduet.
 24|     @app.run    # Start GUI.
 25|   end
 26| 
 27| end
 28| 
 29| Verden.new.start

Innput

  1| require 'fox'
  2| 
  3| class Kalkulator
  4|   include Fox
  5|   def initialize
  6|     @app = FXApp.new
  7|     @main = FXMainWindow.new( @app, "Kalkulator") 
  8|     @felt_x = lag_innputfelt("X")
  9|     @felt_y = lag_innputfelt("Y")
 10|     @utput  = lag_innputfelt("=")
 11|     lag_knapperad
 12|     FXButton.new(@main, "Avslutt", nil, @app, FXApp::ID_QUIT )    
 13|   end
 14|   def lag_innputfelt( merkelapp )
 15|     ramme = FXHorizontalFrame.new( @main ) 
 16|     FXLabel.new( ramme, merkelapp )
 17|     FXTextField.new( ramme, 10, nil, 0, TEXTFIELD_NORMAL | JUSTIFY_RIGHT )
 18|   end
 19|   private :lag_innputfelt
 20|   def lag_knapperad
 21|     ramme = FXHorizontalFrame.new( @main ) 
 22|     %w[ + - * / ** ].each do |tegn|      
 23|       knapp = FXButton.new( ramme, tegn )
 24|       knapp.connect(SEL_COMMAND) do
 25|         beregn( tegn.intern )
 26|       end   
 27|     end
 28|   end
 29|   private :lag_knapperad
 30|   def beregn( symbol )       
 31|     x = @felt_x.text.to_f
 32|     y = @felt_y.text.to_f
 33|     resultat = x.send( symbol, y )
 34|     @utput.text = resultat.to_s
 35|   end
 36|   def start
 37|     @app.create
 38|     @main.show  # Vis vinduet.
 39|     @app.run    # Start GUI.
 40|   end
 41| end
 42| 
 43| Kalkulator.new.start


Next Previous Contents