A simple Tk application in Ruby might look something like this:
require 'tk'
root = TkRoot.new { title "Ex1" }
TkLabel.new(root) {
text 'Hello, World!'
pack { padx 15 ; pady 15; side 'left' }
}
Tk.mainloop
|
Let's look at the code a little more closely.
After loading in the
tk
extension module, we create a
root-level frame using
TkRoot.new
. We then make a label widget
as a child of the root frame, setting several options for the
label. Finally, we pack the root frame and enter the main GUI event loop.
It's a good habit to specify the root explicitly, but you could leave it
out---along with the extra options---and boil this down to a three-liner:
require 'tk'
TkLabel.new { text 'Hello, World!' }
Tk.mainloop
|
That's all there is to it! Armed with one of the Perl/Tk books we
reference at the start of this chapter, you can now produce all the
sophisticated GUIs you need. But then again, if you'd like to stick
around for some more details, here they come.