|
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.
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
|