require"tk"
root =TkRoot.new{ title "Hello, World!"}TkLabel.new(root)do
text "Hello, World!"
pack { padx 15; pady 15; side "left"}endTk.mainloop
Here, after loading the tk extension module, we create a root-level frame using TkRoot.new. We then make a TkLabel 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.
If you would run this script, it would produce the following result −
Ruby/Tk Widget Classes
There is a list of various Ruby/Tk classes, which can be used to create a desired GUI using Ruby/Tk.
Specifies background color to use when drawing active elements. An element is active if the mouse cursor is positioned over the element and pressing a mouse button will cause some action to occur. You can use color names like "red", "blue", "pink", "yellow" etc.
2
activeborderwidth => Integer
Specifies a non-negative value indicating the width of the 3-D border drawn around active elements.
3
activeforeground => String
Specifies foreground color to use when drawing active elements.
4
anchor => String
Specifies how the information in a widget (e.g. text or a bitmap) is to be displayed in the widget. Must be one of the values n, ne, e, se, s, sw, w, nw, or center. For example, nw means display the information such that its top-left corner is at the top-left corner of the widget.
5
background or bg => String
Specifies the normal background color to use when displaying the widget.
6
bitmap => Bitmap
Specifies a bitmap to display in the widget. The exact way in which the bitmap is displayed may be affected by other options such as anchor or justify.
7
borderwidth or bd => Integer
Specifies a non-negative value indicating the width of the 3-D border to draw around the outside of the widget.
8
compound => String
Specifies if the widget should display text and bitmaps/images at the same time, and if so, where the bitmap/image should be placed relative to the text. Must be one of the values none, bottom, top, left, right, or center.
9
cursor => String
Specifies the mouse cursor to be used for the widget. Possible values could be "watch", "arrow" etc.
10
disabledforeground => String
Specifies foreground color to use when drawing a disabled element.
11
exportselection => Boolean
Specifies whether or not a selection in the widget should also be the X selection. The value may have any of the true, false, 0, 1, yes, or no. If the selection is exported, then selecting in the widget deselects the current X selection, selecting outside the widget deselects any widget selection, and the widget will respond to selection retrieval requests when it has a selection.
12
font => String
Specifies the font to use when drawing text inside the widget.
13
foreground or fg => String
Specifies the normal foreground color to use when displaying the widget.
14
highlightbackground => String
Specifies the color to display in the traversal highlight region when the widget does not have the input focus.
15
highlightcolor => String
Specifies the color to use for the traversal highlight rectangle that is drawn around the widget when it has the input focus.
16
highlightthickness => Integer
Specifies a non-negative value indicating the width of the highlight rectangle to draw around the outside of the widget when it has the input focus.
17
image => Image
Specifies an image to display in the widget, which must have been created with an image create. Typically, if the image option is specified then it overrides other options that specify a bitmap or textual value to display in the widget; the image option may be reset to an empty string to re-enable a bitmap or text display.
18
jump => String
For widgets with a slider that can be dragged to adjust a value, such as scrollbars and scales, this option determines when notifications are made about changes in the value. The option"s value must be a boolean. If the value is false, updates are made continuously as the slider is dragged. If the value is true, updates are delayed until the mouse button is released to end the drag; at that point a single notification is made.
19
justify => String
When there are multiple lines of text displayed in a widget, this option determines how the lines line up with each other. Must be one of left, center, or right. Left means that the lines" left edges all line up, center means that the lines" centers are aligned, and right means that the lines" right edges line up.
20
offset => String
Specifies the offset of tiles (see also tile option). It can have two different formats offset x,y or offset side, where side can be n, ne, e, se, s, sw, w, nw, or center.
21
orient => String
For widgets that can lay themselves out with either a horizontal or vertical orientation, such as scrollbars, this option specifies which orientation should be used. Must be either horizontal or vertical or an abbreviation of one of these.
22
padx => Integer
Specifies a non-negative value indicating how much extra space to request for the widget in the X-direction.
23
pady => Integer
Specifies a non-negative value indicating how much extra space to request for the widget in the Y-direction.
24
relief => Integer
Specifies the 3-D effect desired for the widget. Acceptable values are raised, sunken, flat, ridge, and groove.
25
repeatdelay => Integer
Specifies the number of milliseconds a button or key must be held down before it begins to auto-repeat. Used, for example, on the up- and down-arrows in scrollbars.
26
repeatinterval => Integer
Used in conjunction with repeatdelay: once auto-repeat begins, this option determines the number of milliseconds between auto-repeats
27
selectbackground => String
Specifies the background color to use when displaying selected items.
28
selectborderwidth => Integer
Specifies a non-negative value indicating the width of the 3-D border to draw around selected items.
29
selectforeground => String
Specifies the foreground color to use when displaying selected items.
30
setgrid => Boolean
Specifies a boolean value that determines whether this widget controls the resizing grid for its top-level window. This option is typically used in text widgets, where the information in the widget has a natural size (the size of a character) and it makes sense for the window"s dimensions to be integral numbers of these units.
31
takefocus => Integer
Provides information used when moving the focus from window to window via keyboard traversal (e.g., Tab and Shift-Tab). Before setting the focus to a window, the traversal scripts first check whether the window is viewable (it and all its ancestors are mapped); if not, the window is skipped. A value of 0 means that this window should be skipped entirely during keyboard traversal. 1 means that the this window should always receive the input focus.
32
text => String
Specifies a string to be displayed inside the widget. The way in which the string is displayed depends on the particular widget and may be determined by other options, such as anchor or justify.
33
textvariable => Variable
Specifies the name of a variable. The value of the variable is a text string to be displayed inside the widget; if the variable value changes then the widget will automatically update itself to reflect the new value. The way in which the string is displayed in the widget depends on the particular widget and may be determined by other options, such as anchor or justify.
34
tile => Image
Specifies image used to display the widget. If image is the empty string, then the normal background color is displayed.
35
troughcolor => String
Specifies the color to use for the rectangular trough areas in widgets such as scrollbars and scales.
36
troughtile => Image
Specifies image used to display in the rectangular trough areas in widgets such as scrollbars and scales.
37
underline => Integer
Specifies the integer index of a character to underline in the widget. This option is used by the default bindings to implement keyboard traversal for menu buttons and menu entries. 0 corresponds to the first character of the text displayed in the widget, 1 to the next character, and so on.
38
wraplength => Integer
For widgets that can perform word-wrapping, this option specifies the maximum line length.
39
xscrollcommand => function
Specifies a callback used to communicate with horizontal scrollbars.
40
yscrollcommand => function
Specifies a callback used to communicate with vertical scrollbars.