Ruby/Tk Classes and Modules

    This document has been translated from Koji Arai's draft Ruby/Tk document
    (available at http://ns103.net/~arai/ruby/rubytk-20011201.html.gz)

    Nagai Hidetoshi's notes on the original footnotes have been included in italics.

    The translation has the following regrettable features:

      * No attempt was made to fix problems or ambiguities in the original, even 
      obvious ones -- remember the original is a draft.
      * The translation has introduced an unknown but large number of problems and
      ambiguities of its own.
      * Nothing was added or clarified -- all footnotes and comments are from the
      original.
      * No attempt was made to use consistent terms, even when the original did.  
      * No proofreading was done.
      * Jankenpon was used to resolve all doubtful or difficult bits.

    That said, enjoy!

    Benjamin Peterson (http://www.jbrowse.com)

Classes

Modules

Exception Classes

TclTkIp

This is a class for using Tcl/Tk directly.

In Ruby/Tk programming, there is no need to be aware of the existence of this class. For details, see ext/tcltklib/MANUAL.euc

TkCore::INTERP is an instance of this class.

Superclass:

Object

Class Methods:

TclTkIp.new

Creates a Tcl/Tk interpreter object.

Methods:

TclTkIp#_eval(str)

Executes the arbitrary Tcl/Tk script given in str. Returns the result.

TclTkIp#_invoke(cmd, *args)

Executes the arbitrary Tcl/Tk command cmd, passing args as arguments. Returns the result.

TclTkIp#_return_value

Returns 0 if the last call to TclTkIp#_invoke or TclTkIp#_eval executed correctly.

TclTkIp#mainloop

Executes the Tcl/Tk main loop. Same as TclTkLib.mainloop *1

TclTkIp#_toUTF8(str, encodename)
TclTkIp#_fromUTF8(str, encodename)

TkKernel

The superclass of TkObject. The subclass redefines the class method new to take a block.

Superclass:

Object

Class Methods:

TkKernel.new {|obj| ...}

Instantiates the TkKernel object.

If a block is given, the created object will run it as an argument. The context within the block will be such that 'self' is the created object.

In short,

TkKernel.new {|obj| ... }

is equivalent to

object = TkKernel.new
object.instance_eval {|obj| ... }

.

TkKernel.new redefines Class#new, so this property is also seen in subclasses of TkKernel.

TkObject

The superclass of all widgets and ofTkCanvas and TkText.

The subclasses of TkObject contain the following kinds of method:

There is normally no reason to be aware of this distinction in Ruby/Tk programming, but depending on implementation such a case may arise. TkObject#method_missing, TkComposite

You can see the options for a given class at a glance with the following method:

require "tk"
puts TkLabel.new.configinfo.collect {|name,| name}

Superclass:

TkKernel

Included Modules:

Tk

TkTreatFont

TkBindCore

Private Methods:

TkObject#tk_send(cmd、*rest)

Execute cmd on 'self', passing rest as parameters. An implementation of a WidgetCommand.

TkObject#tk_trace_variable(v)

This method simply takes argument v but if v is not a TkVariable object then an ArgumentError exception will be raised. There is an internal type check.

Methods:

TkObject#path

Returns the path of the window. The WidgetCommand's result is applied to this path.

TkObject#epath

Returns the path of the window. The widget arranger TkWindow#pack is applied to this path.

The distinction between path and epath is for the sake of implementing TkComposite. *2

TkObject#to_eval

Returns the representation of self in the Tcl/Tk interpreter. This method is overridden in each object to return:

and more. This method communicates internally with the Tcl/Tk interpreter.

(In this manual, the descriptions of particular to_eval implementations are omitted.)

TkObject#[slot]
TkObject#cget(slot)

Returns the value of Optionsslot.

TkObject#[slot] = value
TkObject#configure(slot, value=None)
TkObject#configure(hash)

Sets the value of Optionsslot to value.

require "tk"
TkLabel.new {
  configure('text', "foobar")
  configure('foreground', "red")
  pack
}
Tk.mainloop

When a hash is passed in, the option that corresponds to each key is set to the given value.

require "tk"
TkLabel.new {
  configure('text' => "foobar", 'foreground' => "red")
  pack
}
Tk.mainloop

TkObject#configure_cmd(slot, value)

Sets option slot to value.value is a Proc object.*3

TkObject#configinfo(slot = nil)

For Optionsslot, returns an array containing each of the 5 elements below:

[Option name, parameter name, class name, default value, actual value]

'Parameter name' is a name which, when set in the Option Database(TkOption), is the same as the option name. 'Class name' here is a name that shows the kind of value (nothing to do with a Ruby class).*4

require "tk"
p TkButton.new.configinfo("foreground")

=> ["foreground", "foreground", "Foreground", "Black", "Black"]

When the argument is omitted, an array of arrays is returned containing the above info for each option.

TkObject#event_generate(context, keys=nil)

Generates event context.

By passing a string known as a "virtual event name" to specify a TkVirtualEvent instance, it is possible to raise a Virtual Event.

For details, please refer to TkCore#event_generate.

TkObject#destroy

If a trace variable is defined, cancels that trace.*5

TkObject#method_missing(id, *args)

Executed when an undefined method is called. (See Object#method_missing)

For the purposes of this method, widget options are treated as methods. The variable id is the id of the called method (or option) and the number of args will be 0 or 1.

When there is 1 argument,

configure id.id2name, args[0]

is run. In other words the method creates Options. In this case, the empty string will be returned.

When there are 0 arguments,

cget id.id2name

is run. In other words, the method refers to Options and returns the value of an option.

Note

In a method implemented by method_missing, you cannot make a call from a subclass using super. Diligence is thus required when inheriting new widget classes from the subclasses of TkObject.

require "tk"

class TkButton2 < TkButton
  def text(val = nil)         # redefine option text
    if val
      super "<#{val}>"        # can't call 'text' on TkButton
    else
      super()                 # can't call 'text' on TkButton
    end
  end
end

TkButton2.new {
  p text "a"
  p text
}

((*error-->*)) -:6:in `text': super: no superclass method `text' (NameError)

Read and set options in the way shown below:

require "tk"

class TkButton2 < TkButton
  def text(val = nil)
    if val
      configure "text", "<#{val}>"        # method_missing :text, val
    else
      cget "text"                 # method_missing :text
    end
  end
end

TkButton2.new {
  p text "a"
  p text
}

=> ""
   "<a>"

Standard Options

Below are the standard Tcl/Tk options at a glance, divided by category. However, not all widgets necessarily support all options.

In the explanations below, only the action taken when 1 argument is supplied is given; however, in all cases if 0 arguments are supplied than the existing value is read. TkObject#method_missing

TkObject#activebackground

Sets the background color for when the widget is Active.

require "tk"
TkButton.new.pack.activebackground "black"
Tk.mainloop

TkObject#activeborderwidth

Sets the border color for when the widget is Active.

TkObject#activeforeground

Sets the foreground color for when the widget is Active.

require "tk"
TkButton.new { text "button" }.pack.activeforeground "red"
Tk.mainloop

TkObject#anchor

Specifies where in the widget text and bitmaps will be positioned. Acceptable values are those below:

TkObject#background
TkObject#bg

Usual background color for the widget.

TkObject#bitmap

Specifies a bitmap. Recently, TkObject#image has been used instead.

TkObject#borderwidth
TkObject#bd

Usual border color for the widget.

TkObject#cursor

Specifies the mouse cursor shape to use.*6

TkObject#disabledforeground

The color to use when the widget is Disabled.

require "tk"
TkButton.new {text "normal";   state text}.pack "fill" => "x"
TkButton.new {text "active";   state text}.pack "fill" => "x"
TkButton.new {text "disabled"; state text}.pack "fill" => "x"
Tk.mainloop

TkObject#exportselection

Specifies, as a boolean, whether the application can share its Selection with other applications. Default is true)。

TkObject#font

Sets text font.*7

TkObject#foreground
TkObject#fg

Sets text (and other foreground) color.

TkObject#highlightbackground

When the widget does not have the Input focus, the color of the frame around the widget.

require "tk"
TkButton.new {highlightbackground "red"; highlightcolor "blue"}.
  pack("padx"=>10,"pady"=>10).focus
TkButton.new {highlightbackground "red"; highlightcolor "blue"}.
  pack("padx"=>10,"pady"=>10)
Tk.mainloop

TkObject#highlightcolor

When the widget does have the Input focus, the color of the frame around the widget.

TkObject#highlightthickness

When the widget does have the Input focus, the thickness of the frame.

require "tk"
TkButton.new {
  highlightbackground "red"
  highlightcolor "blue"
  highlightthickness 10
}.pack("padx"=>10,"pady"=>10).focus
TkButton.new {
  highlightbackground "red"
  highlightcolor "blue"
  highlightthickness 10
}.pack("padx"=>10,"pady"=>10)
Tk.mainloop

TkObject#image

Sets the TkImage.

TkObject#insertbackground

Color of the input cursor.

require "tk"
TkEntry.new {
  insertbackground "red"
  insertwidth 50
  insertborderwidth 10
  insert 0, "foobar"
}.pack.focus
Tk.mainloop

TkObject#insertborderwidth

Specifies the width of the input cursor border. Because the cursor width (set in TkObject#insertwidth) is included, the border width cannot be greater than the cursor width.

TkObject#insertofftime

Together with TkObject#insertontime, sets the cursor's blink speed. This value is the time to spend 'off' in milliseconds.

TkObject#insertontime

Together with TkObject#insertofftime, sets the cursor's blink speed. This value is the time to spend 'on' in milliseconds.

TkObject#insertwidth

Sets the width of the input cursor.TkObject#insertborderwidth

TkObject#jump

A boolean that specifies the behavior of the scrollbar, TkScrollbar. When true, the command associated with the scrollbar is executed every time it moves. When false, the command is executed only when the scroll bar is released.

In the example below, a scroll bar with this value set to true and one with this value set to false control a text input widget (TkEntry) together.

require "tk"

s1 = s2 = nil

e = TkEntry.new {
  xscrollcommand {|arg| s1.set *arg; s2.set *arg}
  insert 0, ("a".."z").to_a.join * 3
}.pack "fill"=>"x"

s1 = TkScrollbar.new {
  jump true
  orient "h"
  command {|arg| e.xview *arg }
}.pack "fill"=>"x"

s2 = TkScrollbar.new {
  jump false
  orient "h"
  command {|arg| e.xview *arg }
}.pack "fill"=>"x"

Tk.mainloop

TkObject#justify

Specifies the text justifying method. Acceptable values are as follows:

###--- TkObject#textvariable

TkObject#troughcolor

Sets the color for concavities.

require "tk"
TkScale.new {
  orient "h"
  troughcolor "red"
  pack "fill"=>"x"
}
TkScrollbar.new {
  orient "h"
  troughcolor "red"
  pack "fill"=>"x"
}
Tk.mainloop

TkObject#underline

Specifies where the text is underlined. This can be used to specify a shortcut key.

require "tk"
TkLabel.new { text "fooBar"; underline 3 }.pack
Tk.mainloop

TkObject#wraplength

For text wrapping, specifies the length of a line. If 0, text cannot be wrapped.

require "tk"
TkLabel.new { text "foobar"; wraplength 20; p wraplength}.pack
Tk.mainloop

###--- TkObject#xscrollcommand

###--- TkObject#yscrollcommand

TkWindow

This is the superclass of all widgets.

Superclass:

TkObject

Extended Modules:

TkBindCore

Class Methods:

TkWindow.new(parent=nil, keys=nil)
TkWindow.new(parent=nil, keys=nil) {|win| ... }

The parent widget will be the widget passed in parent. If parentis omitted, the root widget TkRoot will be the parent.

keys is a hash that specifies the Options . Because the options settable in keys are implemented as methods, both

TkLabel.new(nil, "text"=>"foo").pack

and

label = TkLabel.new
label.text "foo"
label.pack

can be written (pack is not an option).

If a block is given, the created widget will execute the block as an argument. The context within the block will be such that 'self' is the created object.

The example above can also be written thus:

TkLabel.new {
  text "foo"
  pack
}

If a block is used, the parent-child relationship can be clearly expressed visually as below:

require "tk"
TkFrame.new {
  TkLabel.new(self) {
    text "foo"
  }.pack
}.pack
Tk.mainloop

However, since self may change within the block, there is the possibility that the names of instance methods, of Ruby's functions and of local variables may collide. Depending on the actual content, it is also necessary to be careful about legibility.

require "tk"

text = "foo"
p text                # <- This is of course a local variable

TkLabel.new {
  text "bar"
  p text              # <- This is a local variable
  p self.text         # <- This is TkLabel#text
  raise           # <- TkWindow#raise called with no exception
}.pack

Tk.mainloop

Private Methods:

TkWindow#create_self

Creates a widget at the Tcl/Tk level. Called from TkWindow#initialize for initialization.

TkWindow subclasses must override this method to create the relevant widget. In this manual, descriptions of the individual subclasses' create_self methods are omitted.

Usually called in the format

tk_call 'Tcl/Tk command', path

*8

Methods:

TkWindow#pack(keys=nil)

Packs widgets. The packed widgets are arranged according to the behavior specified in keys(a {name=>value...} hash). Returns self.

Tk.pack self, keys

is the same as

TkPack.configure self, keys

See TkPack.

TkWindow#unpack

Releases a widget arranged with 'pack' (so they are no longer visible). Returns self.

TkWindow#grid(keys=nil)

Arranges widgets at the intersections of the grid specified in keys. Returns self.

Tk.grid self, keys

is the same as

TkGrid.configure self, keys

See TkGrid.

TkWindow#ungrid

Releases a widget arranged with 'grid' (so they are no longer visible). Returns self.

TkWindow#place(keys=nil)

Places the widget at the location given in keys. Returns self.

keys is a hash containing the following keys:

TkWindow#unplace
TkWindow#place_forget

Releases a widget arranged with 'place' (so they are no longer visible). Returns self.

TkWindow#place_config(keys)

Repositions a widget arranged with 'place' according to the values specified in keys.*9

TkWindow#place_info

Returns the position information of a widget positioned with 'place' as a hash.

TkWindow#pack_slaves

Returns an array of all the widgets packed within the self widget.

TkWindow#pack_info

Returns the position information of a widget positioned with 'pack' as a hash.

TkWindow#place_slaves

Returns an array of all the widgets arranged with 'place' within the self widget.

TkWindow#focus(force = false)

Sets Input focus to self. If forceis true, then the focus will be obtained even if another application has it. Returns self.

TkWindow#grab(arg = nil)

Performs grab-related operations on the widget. When the widget is grabbed, events are not raised with other widgets.

TkWindow#lower(below=None)

Puts the widget below the below widget. When below is omitted, the widget is put at the bottom. Returns self.

TkWindow#raise(above=None)

Puts the widget above the above widget. When above is omitted, the widget is put at the top. Returns self.

Note

In a context where 'self' is an instance of TkWindow,Kernel#raise cannot be used to raise an exception. In Ruby/Tk programming, it is safer to useKernel#fail to raise an exception.

TkWindow#command(cmd=Proc.new)

Register the Proc object given in the argument as a command.

TkButton.new {
  text "button"
  command { puts "button" }
  pack
}
Tk.mainloop

TkWindow#colormodel(model=None)

(This method cannot be used in Tk versions greater than X.X)*10

Specify whether the Ruby/Tk widget's color model should be color or black and white. Without arguments, returns the widget's color model.

Acceptable values for model are as follows:

Returns self.

TkWindow#destroy

Totally destroys the widget.

TkWindow#wait
TkWindow#wait_visibility

Waits for the widget's visibility to change.

TkWindow#wait_destroy

Waits until TkWindow#destroy destroys the widget.

TkWindow#bindtags(taglist=nil)

Specifies the self widget's Taglist as taglist. The taglist is an array showing the propagation of events. It's elements are:

. When the self widget raises an event, each element in the list is found in order, and the appropriate Event Callback Functions are all executed.

Without arguments, return's the widget's taglist.

By default, the taglist consists of:

require "tk"
b1 = TkButton.new
p b1.bindtags

TkToplevel.new {
  TkButton.new(self) {
    p bindtags
  }
}

=> [#<TkButton: @path=".w0000">, TkButton, #<TkRoot: @path=".">, "all"]
   [#<TkButton: @path=".w0002.w0003">, TkButton, \
    #<TkToplevel: @screen=nil, @path=".w0002", @classname=nil>, "all"]

The following is an example of the definition of a taglist:

require "tk"
b1 = TkButton.new.pack
b1.bindtags [b1, Tk.root, "all"]
p b1.bindtags
Tk.mainloop

=> [#<TkButton: @path=".w0000">, #<TkRoot: @path=".">, "all"]

If the widget class is omitted from the list in this way, then a button will not take on the 'pressed' state even if right-clicked. This is because the Event callback function for this action is registered on the button widget class.

By default, in the "all" tag,

require "tk"
p Tk.bindinfo "all"

=> ["Shift-Key-Tab", "Key-Tab", "Key-F10", "Alt-Key"]

actions for the above key events are defined. Accordingly, it is clear that if the "all" tag is not removed from the tag list, actions for these key presses will be defined on all widgets.

Note

Strings can also be specified as elements in the tag list. However, the 'all' widget path and widget class name are handled specially.

If a widget's path name is given in a tag, the effect is the same as if the widget itself were given. In the same way, giving the widget class name (in the case of TkButton, "Button") is the same as giving the widget class. When specifying tags as strings, care must be taken to avoid name collisions. (In ruby-1.4.4, in tk.rb the class TkBindTag was provided for defining unique tags.)

require "tk"
newtag = TkBindTag.new
TkButton.new {
  bindtags [path, "Button", newtag]
  p bindtags
}

=> [#<TkButton: @path=".w0000">, TkButton, #<TkBindTag: btag00000>]

TkRoot

This class represents the root widget. The root widget is at the top of the Ruby/Tk widget hierarchy

Superclass:

TkWindow

Included Modules:

Tk::Wm

Class Methods:

TkRoot.new

Instantiates the root widget object. Always returns the same object. (Only 1 root widget can exist at once in Ruby/Tk)

Methods:

TkRoot#path

Returns the root widget's path, which must be "." and nothing else.

Standard Options:

TkObject#borderwidth TkObject#bd

TkObject#cursor

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#relief

TkObject#takefocus

Options:

TkRoot#background(color)
TkRoot#bg(color)

### --- TkRoot#class ### --- TkRoot#classname

TkRoot#colormap
TkRoot#container(bool)
TkRoot#height
TkRoot#menu
TkRoot#screen
TkRoot#use
TkRoot#visual
TkRoot#width

TkToplevel

Superclass:

TkWindow

Included Modules:

Tk::Wm

Class Methods:

TkToplevel.new(parent=nil, keys=nil)
TkToplevel.new(parent=nil, screen=nil, classname=nil, keys=nil)

Methods:

TkToplevel#specific_class

Standard Options:

TkObject#borderwidth TkObject#bd

TkObject#cursor

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#relief

TkObject#takefocus

Options:

TkToplevel#background(color)
TkToplevel#bg(color)

### --- TkToplevel#class

TkToplevel#classname
TkToplevel#colormap
TkToplevel#container(bool)
TkToplevel#height
TkToplevel#menu
TkToplevel#screen
TkToplevel#use
TkToplevel#visual
TkToplevel#width

TkFrame

A widget whose only purpose is to position other widgets.

To arrange widgets in a grid with TkWindow#pack, you must use the frame widget.

require "tk"

TkFrame.new {
  TkLabel.new(self, {"text"=>"A"}).pack "side"=>"left"
  TkLabel.new(self, {"text"=>"B"}).pack "side"=>"left"
}.pack
TkFrame.new {
  TkLabel.new(self, {"text"=>"C"}).pack "side"=>"left"
  TkLabel.new(self, {"text"=>"D"}).pack "side"=>"left"
}.pack
Tk.mainloop

Superclass:

TkWindow

Standard Options:

TkObject#borderwidth TkObject#bd

TkObject#cursor

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#relief

TkObject#takefocus

Options:

TkFrame#background(color)
TkFrame#bg(color)

Set the frame's color to color. On TkFrame, this option has a special value, "", meaning no color. *11

### --- TkFrame#class

TkFrame#classname

In Tcl/Tk this option is called class, but since that collides with a Ruby reserved word it is defined as classname*12.

This option can only be specified by being passed to new when the object is created. To find the value, use the instance variable @classname.

TkFrame#colormap

This option can only be specified by being passed to new when the object is created. To find the value, use the instance variable of the same name.

TkFrame#container(bool)

This option can only be specified by being passed to new when the object is created. To find the value, use the instance variable of the same name.

TkFrame#height(h)

h is the height of the frame. If h is 0, the height of the frame depends on the widgets positioned within it.

TkFrame#visual

This option can only be specified by being passed to new when the object is created. To find the value, use the instance variable of the same name.

TkFrame#width(w)

w is the width of the frame. If w is 0, the width of the frame depends on the widgets positioned within it.

TkMenubar

Superclass:

TkFrame

Included Modules:

TkComposite

Class Methods:

TkMenubar.new(parent = nil, spec = nil, options = nil)

Methods:

TkMenubar#add_menu(menu_info)
TkMenubar#[index]

TkLabel

This widget represents a label.

require "tk"
TkLabel.new {
  text "label"
  pack
}
Tk.mainloop

Superclass:

TkWindow

Methods:

TkLabel#textvariable(var)

The TkVariable instance var's content is made into a label. If the value of var changes, the label's contents automatically change as well.

Standard Options:

TkObject#anchor

TkObject#background

TkObject#bitmap

TkObject#borderwidth

TkObject#cursor

TkObject#font

TkObject#foreground

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#image

TkObject#justify

TkObject#padx

TkObject#pady

TkObject#relief

TkObject#takefocus

TkObject#text

### TkObject#textvariable

TkObject#underline

TkObject#wraplength

Options:

TkLabel#height
TkLabel#width

TkButton

require "tk"
TkButton.new {
  text "button"
  command { puts "button" }
  pack
}
Tk.mainloop

Superclass:

TkLabel

Methods:

TkButton#invoke

Executes the command option (A TkWindow#command specified with a Proc object).

This allows you to programmatically execute the same functionality that would be invoked on an actual button press.

TkButton#flash

Makes the button flash.

Standard Options:

TkObject#activebackground

TkObject#activeforeground

TkObject#anchor

TkObject#background

TkObject#bitmap

TkObject#borderwidth

TkObject#cursor

TkObject#disabledforeground

TkObject#font

TkObject#foreground

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#image

TkObject#justify

TkObject#padx

TkObject#pady

TkObject#relief

TkObject#takefocus

TkObject#text

### TkObject#textvariable

TkObject#underline

TkObject#wraplength

Options:

### --- TkButton#command

TkButton#default

This method was implemented in Tk version 8.0

One of the 3 values below must be specified:

TkButton#height

Sets the button's height. If the button contains TkObject#text then the number of lines is set; if it contains an TkObject#image then the pixel size of the image is set. (See distance )

TkButton#state

One of the 3 values below must be specified:

TkButton#width

Sets the button's width. If the button contains TkObject#text then the number of characters is set; if it contains an TkObject#image then the pixel size of the image is set. (See distance )

TkRadioButton

require "tk"
v  = TkVariable.new
c = proc {print v, "\n"}
TkRadioButton.new {text "a"; variable v; value 1; select;   command c; pack}
TkRadioButton.new {text "b"; variable v; value 2; deselect; command c; pack}
TkRadioButton.new {text "c"; variable v; value 3; deselect; command c; pack}
Tk.mainloop

Superclass:

TkButton

Methods:

TkRadioButton#deselect

Puts the radion button into the unchecked state.

TkRadioButton#select

Puts the radio button into the checked state.

TkRadioButton#variable(v)

Links the radio button's state to the TkVariable v. Radio buttons linked to the same TkVariable are in the same group. When the radio button is checked, v takes on the value given in TkRadioButton#value.

TkRadioButton#value(val)

Sets the value that the radio button will have when checked to val.

Standard Options:

TkObject#activebackground

TkObject#activeforeground

TkObject#anchor

TkObject#background

TkObject#bitmap

TkObject#borderwidth

TkObject#cursor

TkObject#disabledforeground

TkObject#font

TkObject#foreground

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#image

TkObject#justify

TkObject#padx

TkObject#pady

TkObject#relief

TkObject#takefocus

TkObject#text

### TkObject#textvariable

TkObject#underline

TkObject#wraplength

Options:

### --- TkRadioButton#command

TkRadioButton#height

See TkButton#height

TkRadioButton#indicatoron
TkRadioButton#selectcolor
TkRadioButton#selectimage
TkRadioButton#state

### --- TkRadioButton#value

### --- TkRadioButton#variable

TkRadioButton#width

TkButton#width参照

TkCheckButton

This class is for CheckButton widgets.

The variable option specifies a variable that will be linked to the widget. The values to which this variable will be set can be changed via TkCheckButton#onvalue and TkCheckButton#offvalue . By default, the ON state has a value of 1 and the OFF state is 0.

require "tk"

v1 = TkVariable.new
v2 = TkVariable.new
v3 = TkVariable.new

c = proc {print v1.value,v2.value,v3.value,"\n"}

TkCheckButton.new {text "a"; variable v1; command c; pack}
TkCheckButton.new {text "b"; variable v2; command c; pack}
TkCheckButton.new {text "c"; variable v3; command c; pack}
Tk.mainloop

Superclass:

TkRadioButton

Methods:

TkCheckButton#toggle

Toggles the check button's ON/OFF state.

Standard Options:

TkObject#activebackground

TkObject#activeforeground

TkObject#anchor

TkObject#background

TkObject#bitmap

TkObject#borderwidth

TkObject#cursor

TkObject#disabledforeground

TkObject#font

TkObject#foreground

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#image

TkObject#justify

TkObject#padx

TkObject#pady

TkObject#relief

TkObject#takefocus

TkObject#text

### TkObject#textvariable

TkObject#underline

TkObject#wraplength

Options:

###--- TkCheckButton#command

TkCheckButton#height

TkButton#height参照

TkCheckButton#indicatoron
TkCheckButton#offvalue

Gives the value for when the check button is unchecked.

TkCheckButton#onvalue

Gives the value for when the check button is checked.

TkCheckButton#selectcolor
TkCheckButton#selectimage
TkCheckButton#state

### --- TkCheckButton#variable

TkCheckButton#width

See TkButton#width

TkEntry

In the explanations of the methods below, location is one of the following:

*25

Please contemplate the different actions of each mode below:

require "tk"

list = TkListbox.new {
  ("a".."z").each {|c| insert 'end', c}
}.pack 'fill'=>'both', 'expand'=>true

mode_var = TkVariable.new("browse")       # default mode

TkLabel.new {
  text "select one of below modes"
}.pack 'fill'=>'x'

%w(single browse multiple extended).each {|mode|
  TkRadioButton.new {
    text mode
    variable mode_var
    value mode
    command {
      list.selectmode mode
    }
  }.pack 'side'=>'left', 'fill'=>'x', 'expand'=>true
}
Tk.mainloop

TkListbox#width

Set the width using a string. If 0 is given, the width will be automatically adjusted so that all strings fit (in this case, the value read will be 0)。

require "tk"
TkListbox.new {
 width 0
 insert "end", "a" * 10
 insert "end", "b"
 insert "end", "c"
 p width
}.pack
Tk.mainloop

If the font is not fixed-width, using the string "0" is the norm.

TkScrollbox

A widget that combines a Tk::Scrollbar with a TkListbox. This class is an example of the usage of the TkComposite module.

Superclass:

TkListbox

Included Modules:

TkComposite

TkText

location

Superclass:

TkTextWin

Included Modules:

TkTreatTextTagFont

Scrollable

Class Methods:

TkText.new(*args)

Methods:

TkText#init_instance_variable
TkText#index(index)
TkText#value
TkText#value= (val)
TkText#_addcmd(cmd)
TkText#_addtag(name, obj)
TkText#tagid2obj(tagid)
TkText#tag_names(index=None)
TkText#mark_names
TkText#window_names
TkText#image_names
TkText#set_insert(index)
TkText#set_current(index)
TkText#insert(index, chars, *tags)
TkText#destroy
TkText#backspace
TkText#compare(idx1, op, idx2)
TkText#debug
TkText#debug=(boolean)
TkText#bbox(index)
TkText#dlineinfo(index)
TkText#yview(*what)
TkText#yview_pickplace(*what)
TkText#xview(*what)
TkText#xview_pickplace(*what)
TkText#tag_add(tag, index1, index2=None)
TkText#tag_bind(tag, seq, cmd=Proc.new, args=nil)
TkText#tag_bind_append(tag, seq, cmd=Proc.new, args=nil)
TkText#tag_bindinfo(tag, context=nil)
TkText#tag_cget(tag, key)
TkText#tag_configure(tag, key, val=None)
TkText#tag_configinfo(tag, key=nil)
TkText#tag_raise(tag, above=None)
TkText#tag_lower(tag, below=None)
TkText#tag_remove(tag, *index)
TkText#tag_ranges(tag)
TkText#tag_nextrange(tag, first, last=None)
TkText#tag_prevrange(tag, first, last=None)
TkText#search_with_length(pat,start,stop=None)
TkText#search(pat,start,stop=None)
TkText#rsearch_with_length(pat,start,stop=None)
TkText#rsearch(pat,start,stop=None)

Standard Options:

TkObject#background

TkObject#borderwidth

TkObject#cursor

TkObject#exportselection

TkObject#font

TkObject#foreground

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#insertbackground

TkObject#insertborderwidth

TkObject#insertofftime

TkObject#insertontime

TkObject#insertwidth

TkObject#padx

TkObject#pady

TkObject#relief

TkObject#selectbackground

TkObject#selectborderwidth

TkObject#selectforeground

TkObject#setgrid

TkObject#takefocus

### TkObject#xscrollcommand

### TkObject#yscrollcommand

Options:

TkText#height
TkText#spacing1
TkText#spacing2
TkText#spacing3
TkText#state
TkText#tabs
TkText#width
TkText#wrap

TkMenu

Also see TkMenubutton

require "tk"

v  = TkVariable.new
v1 = TkVariable.new
v2 = TkVariable.new
v3 = TkVariable.new

menu = TkMenu.new {
  title "menu entries"
  tearoff false
  c = proc { p self.entryconfiginfo "end"}
  add "command", {"label"=>"hoge", "command"=>c}
  add "separator"
  add "checkbutton", {"label"=>"hoge1", "command"=>c, "variable"=>v1, "onvalue"=>1}
  add "checkbutton", {"label"=>"hoge2", "command"=>c, "variable"=>v2}
  add "checkbutton", {"label"=>"hoge3", "command"=>c, "variable"=>v3}
  add "separator"
  add "radiobutton", {"label"=>"hoge1", "command"=>c, "variable"=>v}
  add "radiobutton", {"label"=>"hoge2", "command"=>c, "variable"=>v}
  add "radiobutton", {"label"=>"hoge3", "command"=>c, "variable"=>v}
  20.times {|i| p menutype i}
  activate 0
}

Tk.root.bind('Button-1') {|e| menu.post(e.x_root, e.y_root) }

Tk.mainloop

The location mentioned in the method explanations below is one of the following:

Superclass:

TkWindow

Included Modules:

TkTreatMenuEntryFont

Methods:

TkMenu#activate(index)

Makes the element at the location given in index active.

TkMenu#add(type, keys=nil)

Add one of the kind of item specified by type. type is one of the following:

keys is a hash that contains options for the menu item. The possible options are below:

TkMenu#index(index)

Returns the index number of the element whose location is given in index.

TkMenu#invoke(index)

Execute the item whose location is given in index.

TkMenu#insert(index, type, keys=nil)

Insert an item at the location given in index. type and keys are as in TkMenu#add

TkMenu#delete(index, last=None)

Deletes the item whose location is given in index. If last is specified, items from index to last are deleted.

TkMenu#post(x, y)

Positions self at the given coordinates.

require "tk"
menu = TkMenu.new {
  add 'command', {'label'=>'foo1', 'command'=>proc { puts '1'}}
  add 'command', {'label'=>'foo2', 'command'=>proc { puts '2'}}
  add 'command', {'label'=>'foo3', 'command'=>proc { puts '3'}}
}
Tk.root.bind 'Button-3', proc {|e| menu.post e.x_root, e.y_root }
Tk.root.bind 'Button',   proc {|e| menu.unpost }
Tk.mainloop

*27

TkMenu#postcascade(index)

Position a cascade menu at the location given in index. *28

TkMenu#postcommand(cmd=Proc.new)

When self is repositioned, execute cmd. Called from the TkMenu#post method.

TkMenu#menutype(index)

*29

TkMenu#unpost

Make self no longer have a position -- it will no longer be displayed.

TkMenu#yposition(index)

Returns the position pixels of the item whose location is given in index.

TkMenu#entrycget(index, key)

Return the value of the option given in key, for the item whose location is given in index.

TkMenu#entryconfiure(index, key, val=None)
TkMenu#entryconfiure(index, hash)

Sets the value of option key to val, for the item whose location is given in index.

If a hash is given in the 2nd argument, the keys are treated as option names and the values as option values.

TkMenu#entryconfiginfo(index, key=nil)

Sets the value of option key to val, for the item whose location is given in index.

Standard Options

TkObject#activebackground

TkObject#activeborderwidth

TkObject#activeforeground

TkObject#background

TkObject#borderwidth

TkObject#cursor

TkObject#disabledforeground

TkObject#font

TkObject#foreground

TkObject#relief

TkObject#takefocus

Options

### --- TkMenu#postcommand

TkMenu#selectcolor
TkMenu#tearoff
TkMenu#tearoffcommand
TkMenu#title

### --- TkMenu#type

TkScale

require "tk"

TkScale.new {
  orient "horizontal"
  length 200
  from 1
  to 500
  command {|val|
    p val
  }
  pack
}
Tk.mainloop

Superclass:

TkWindow

Methods:

TkScale#get
TkScale#value

Obtains the value of the scale.

TkScale#set(val)
TkScale#value=(val)

Sets the value of the scale to val.

Standard Options:

TkObject#activebackground

TkObject#background

TkObject#borderwidth

TkObject#cursor

TkObject#font

TkObject#foreground

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#orient

TkObject#relief-troughcolor

TkObject#repeatdelay

TkObject#repeatinterval

TkObject#takefocus

Options:

TkScale#bigincrement
TkScale#command
TkScale#digits

Valid digits

TkScale#from(n)

Set the topmost (leftmost) value to n.

TkScale#label(text)

Set the scale widget's label to text.

TkScale#length(len)

Set the scale widget's length.

TkScale#resolution(n)

Sets the size of the smallest possible increment on the scale. The value of the scale will be a multiple of this.

TkScale#showvalue(bool)

Sets whether to show the value (bool="1") or hide it (bool="0").

TkScale#sliderlength(len)

Set the slider length to len.

TkScale#sliderrelief(s)

Set the form of the slider. The value used is one of the following:

*30

TkScale#state
TkScale#tickinterval(n)

Set the interval between gradations on the scale. If n is 0, no ticks are shown.

TkScale#to(n)

Sets the bottommost (rightmost) value to n.

TkScale#variable(v)

Links the TkVariable object v to the scale.

TkScale#width

TkCanvas

require "tk"

TkCanvas.new {
  xscrollincrement 50
  yscrollincrement 50
  scrollregion [10, 10, 500, 500]
  xscrollbar(TkScrollbar.new).grid 'column'=>0, 'row'=>1, 'sticky'=>'ew'
  yscrollbar(TkScrollbar.new).grid 'column'=>1, 'row'=>0, 'sticky'=>'ns'

  TkcOval.new(self, 40, 40, 240, 340)
  grid 'column'=>0, 'row'=>0, 'sticky'=>'nsew'
}
TkGrid.columnconfigure Tk.root, 0, 'weight'=>100
TkGrid.rowconfigure    Tk.root, 0, 'weight'=>100

Tk.mainloop

In the method explanations below, location is one of the following:

Superclass:

TkWindow

Included Modules:

TkTreatCItemFont

Tk::Scrollable

Private Methods:

TkCanvas#tagid(tag)

Methods:

TkCanvas#addtag(tag, mode, *args)
TkCanvas#addtag_above(tagOrId, target)
TkCanvas#addtag_all(tagOrId)
TkCanvas#addtag_below(tagOrId, target)
TkCanvas#addtag_closest(tagOrId, x, y, halo=None, start=None)
TkCanvas#addtag_enclosed(tagOrId, x1, y1, x2, y2)
TkCanvas#addtag_overlapping(tagOrId, x1, y1, x2, y2)
TkCanvas#addtag_withtag(tagOrId, tag)
TkCanvas#bbox(tagOrId, *tags)
TkCanvas#itembind(tag, context, cmd=Proc.new, args=nil)
TkCanvas#itembind_append(tag, context, cmd=Proc.new, args=nil)
TkCanvas#itembindinfo(tag, context=nil)
TkCanvas#canvasx(x, *args)
TkCanvas#canvasy(y, *args)
TkCanvas#coords(tag, *args)
TkCanvas#dchars(tag, first, last=None)
TkCanvas#delete(*args)
TkCanvas#remove(*args)
TkCanvas#dtag(tag, tag_to_del=None)
TkCanvas#find(mode, *args)

Returns the specified item as an array.

Values of mode:

TkCanvas#find_above(target)

Return the item above the one given in target as an array of elements.

TkCanvas#find_all

Return all items as an array.

require "tk"

x1 = 10
y1 = 10
x2 = 250
y2 = 200
x3 = (x2-x1)/2 + x1
y3 = (y2-y1)/2 + y1
TkCanvas.new {
  TkcArc.new       self, x1, y1, x2, y2, 'width'=>2.0
  TkcBitmap.new    self, x3, y3, 'bitmap'=>'error'
  # TkcImage.new   self, x3, y3, 'image'=>TkImage.new('bitmap'=>'error')
  TkcLine.new      self, x1, y1, x2, y2, 'arrow'=>'both'
  TkcOval.new      self, x1, y1, x2, y2
  TkcPolygon.new   self, x3, y1, x2, y3, x3, y2, x1, y3,
         'fill'=>'', 'outline'=>'black'
  TkcRectangle.new self, x1, y1, x2, y2
  TkcText.new      self, x2+20, y2-20, 'text'=>"foo\nbar"
  TkcWindow.new    self, x2, y2, 'window'=>TkButton.new

# puts find_all

pack
}

Tk.mainloop

=> #<TkcArc:0x4036fd50>
   #<TkcBitmap:0x4036fa08>
   #<TkcLine:0x4036f648>
   #<TkcOval:0x4036f60c>
   #<TkcPolygon:0x4036eec8>
   #<TkcRectangle:0x4036eeb4>
   #<TkcText:0x4036e734>
   #<TkcWindow:0x4036e4b4>

((-how to specify an image?-))

TkCanvas#find_below(target)

Return the item below the one given in target as an array of elements.

TkCanvas#find_closest(x, y, halo=None, start=None)
TkCanvas#find_enclosed(x1, y1, x2, y2)

Return the item inside the given rectangle as an array of elements.

TkCanvas#find_overlapping(x1, y1, x2, y2)

Return the item that best fits the given rectangle as an array of elements. Any item that would be found by find_enclosed will be found by find_overlapping.

TkCanvas#find_withtag(tag)
TkCanvas#itemfocus(tagOrId=nil)
TkCanvas#gettags(tagOrId)
TkCanvas#icursor(tagOrId, index)
TkCanvas#index(tagOrId, index)
TkCanvas#insert(tagOrId, index, string)
TkCanvas#itemcget(tagOrId, option)
TkCanvas#itemconfigure(tagOrId, key, value=None)
TkCanvas#itemconfiginfo(tagOrId, key=nil)
TkCanvas#lower(tag, below=None)
TkCanvas#move(tag, x, y)
TkCanvas#postscript(keys)
TkCanvas#raise(tag, above=None)
TkCanvas#scale(tag, x, y, xs, ys)
TkCanvas#scan_mark(x, y)
TkCanvas#scan_dragto(x, y)
TkCanvas#select(mode, *args)
TkCanvas#select_adjust(tagOrId, index)
TkCanvas#select_clear
TkCanvas#select_from(tagOrId, index)
TkCanvas#select_item
TkCanvas#select_to(tagOrId, index)
TkCanvas#itemtype(tag)
TkCanvas#xview(*index)
TkCanvas#yview(*index)

Standard Options:

TkObject#background

TkObject#borderwidth

TkObject#cursor

TkObject#highlightbackground

TkObject#highlightcolor

TkObject#highlightthickness

TkObject#insertbackground

TkObject#insertborderwidth

TkObject#insertofftime

TkObject#insertontime

TkObject#insertwidth

TkObject#relief

TkObject#selectbackground

TkObject#selectborderwidth

TkObject#selectforeground

TkObject#takefocus

### TkObject#xscrollcommand

### TkObject#yscrollcommand

Options:

TkCanvas#closeenough
TkCanvas#confine
TkCanvas#height
TkCanvas#scrollregion
TkCanvas#width
TkCanvas#xscrollincrement
TkCanvas#yscrollincrement

TkDialog

require "tk"
dialog = TkDialog.new('message'=>"test", 'buttons'=>["b1","b2","b3"])
p dialog.value

Superclass:

TkWindow

Extended Modules:

Tk

Class Methods:

TkDialog.new(keys = nil)

Creates a dialog box object. A top level window is displayed, and queries the user for information based on the values specified in keys.

When the user replies to a dialog box, a dialog object is returned. In this way, using TkDialog#value, it is possible to tell what button was pressed.

Methods:

TkDialog#value

Returns which button was pressed, as a number. Buttons are counted from the left starting at 0.

TkDialog#title

Returns the initial value of the dialog box's title ("DIALOG"). The intention is for this method to be overridden in subclasses.

*31

TkDialog#message

Returns the initial value of the dialog box's message ("MESSAGE"). The intention is for this method to be overridden in subclasses.

TkDialog#message_config

Returns the initial value of the dialog box's message settings (nil). The intention is for this method to be overridden in subclasses.

Please override this method to return a hash.

TkDialog#bitmap

Returns the initial value of the dialog box's bitmap ("info"). The intention is for this method to be overridden in subclasses.

*32

TkDialog#bitmap_config

Returns the initial value of the dialog box's bitmap settings (nil). The intention is for this method to be overridden in subclasses.

TkDialog#default_button

Returns the initial value of the dialog box's default button (0). The intention is for this method to be overridden in subclasses.

TkDialog#buttons

Returns the initial value of the dialog box's buttons. The intention is for this method to be overridden in subclasses.

Please override this method to return an array whose elements consist of the text of each button.

TkDialog#button_configs(num)

Returns the initial value of the dialog box's num-th button settings. The intention is for this method to be overridden in subclasses.

TkWarning

Superclass:

TkDialog

Class Methods:

TkWarning.new(mes)

Methods:

*33

TkWarning#message
TkWarning#title
TkWarning#bitmap
TkWarning#default_button
TkWarning#buttons

TkcItem

Superclass:

TkObject

Included Modules:

TkcTagAccess

Class Methods:

TkcItem.type2class(type)

Returns a class determined by type.

type may be any one of:

TkcItem.id2obj(id)
TkcItem.new(parent, *args)

Methods:

TkcItem#id
TkcItem#delete

TkcArc

Superclass:

TkcItem

Methods:

TkcBitmap

Superclass:

TkcItem

Methods:

TkcImage

Superclass:

TkcItem

Methods:

TkcLine

Superclass:

TkcItem

Methods:

TkcOval

Superclass:

TkcItem

Methods:

TkcPolygon

Superclass:

TkcItem

Methods:

TkcRectangle

Superclass:

TkcItem

Methods:

TkcText

Superclass:

TkcItem

Methods:

TkcWindow

Superclass:

TkcItem

Methods:

TkcTag

Superclass:

TkObject

Included Modules:

TkcTagAccess

Class Methods:

TkcTag.id2obj(id)
TkcTag.new(parent, mode=nil, *args)

Methods:

TkcTag#id
TkcTag#delete
TkcTag#set_to_above(target)
TkcTag#set_to_all
TkcTag#set_to_below(target)
TkcTag#set_to_closest(x, y, halo=None, start=None)
TkcTag#set_to_enclosed(x1, y1, x2, y2)
TkcTag#set_to_overlapping(x1, y1, x2, y2)
TkcTag#set_to_withtag(target)

TkcGroup

Superclass:

TkcTag

Methods:

TkcGroup#include(*tags)
TkcGroup#exclude(*tags)

TkcTagAll

Superclass:

TkcTag

Class Methods:

TkcTagAll.new(parent)

TkcTagCurrent

Superclass:

TkcTag

Class Methods:

TkcTagCurrent.new(parent)

TkImage

Superclass:

TkObject

Included Modules:

Tk

Class Methods:

TkImage.names
TkImage.types
TkImage.new(keys=nil)

Methods:

TkImage#delete
TkImage#height
TkImage#itemtype
TkImage#width

TkBitmapImage

Superclass:

TkImage

Class Methods:

TkBitmapImage.new(*args)

TkPhotoImage

Superclass:

TkImage

Class Methods:

TkPhotoImage.new(*args)

Methods:

TkPhotoImage#blank
TkPhotoImage#cget(option)
TkPhotoImage#copy(source, *opts)
TkPhotoImage#get(x, y)
TkPhotoImage#put(data, *to)
TkPhotoImage#read(file, *opts)
TkPhotoImage#redither
TkPhotoImage#write(file, *opts)

TkTextImage

Superclass:

TkObject

Class Methods:

TkTextImage.new(parent, index, keys)

Methods:

TkTextImage#[slot]
TkTextImage#[slot] = value
TkTextImage#cget(slot)
TkTextImage#configure(slot, value=None)
TkTextImage#image
TkTextImage#image=(value)
TkTextImage#configinfo(slot = nil)

TkTextMark

Superclass:

TkObject

Class Methods:

TkTextMark.new(parent, index)

Methods:

TkTextMark#id
TkTextMark#set(where)
TkTextMark#unset
TkTextMark#gravity
TkTextMark#gravity=(direction)
TkTextMark#next(index)
TkTextMark#previous(index)

TkTextMarkAnchor

Superclass:

TkTextMark

Class Methods:

TkTextMarkAnchor.new(paren