Ruby/Basic GUI

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
** Ruby Callbacks, Events, and Srolling
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

[edit] Ruby Basic GUI

The Tk extension for Ruby provides it with GUI(graphical user interface) capabilities. You will have to know at least the basics of Tk in order to program applications in Ruby that support it.Double check you have tcl/tk installed.You will get an error otherwise when you try to run your program.

If you've used InstantRails for example, Install ActiveTcl from http://www.activestate.com/Products/ActiveTcl/ Some tk related files are no longer bundled together with one-click install packages.

When writing such an application, you need to instruct Ruby that it needs to use Tk by writing "require 'tk'" and write "Tk.mainloop" when you want to start the GUI. For Mac OS X you can use RubyCocoa for graphical user interfaces but I won't be covering it in this tutorial.

Here is a simple hello world program:

require 'tk'
root = TkRoot.new { title "Hello world" }
TkLabel.new(root) do
text 'Hello world!'
end
Tk.mainloop

or even more simple:

require 'tk'
  msg = TkRoot.new { title “Hello World!” }
Tk.mainloop

You probably should switch back between Tk tutorials and this one if you're really interested in GUIs.Here's a comprehensive Perl/Tk tutorial that you can skim through online.

What the code does:

After Ruby loads the tk extension module, a root-level frame using TkRoot.new is created.A TkLabel widget is created as a child of the root frame.


Image:Hello.gif

Tk is very powerful even though it might be too simplistic at first. Here's another interesting thing you can do with it: Creating widgets and binding code to them.The first code sample above contains a widget created with TkLabel.

Here's a more complex code:

require 'tk'
 
root = TkRoot.new do
title "Hello world!" 
minsize(250,250)
end
 
TkLabel.new(root) do
text 'Hello world!'
background 'blue'
pack { padx 15; pady 15; side ‘left’}
end
 
Tk.mainloop

Image:Hello2.gif

Previous Next