Class: RubyLabs::Canvas::Text

Inherits:
TkObject
  • Object
show all
Defined in:
lib/rubylabs.rb

Overview

Text

A Text object is a string displayed on the RubyLabs canvas.

Instance Attribute Summary

Attributes inherited from TkObject

#coords, #name, #penpoint

Instance Method Summary collapse

Methods inherited from TkObject

#erase, #fill=, #lower, nextId, options, #raise, reset

Constructor Details

#initialize(s, x, y, args = {}) ⇒ Text

Display string s at location (x,y) on the canvas. By default the coordinates specify the location of the top left of the first character in the string (in Tk terminology, the anchor point for the text is “northwest”). A different anchor position and other attributes of the text can be passed in a hash object at the end of the argument list.

Example – display a message in dark green letters:

>> x = Canvas::Text.new("hello", 50, 50, :fill => 'darkgreen')
=> #<RubyLabs::Canvas::Text: ... >

:call-seq:

x = Text.new(s, x, y, args = {})


1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# File 'lib/rubylabs.rb', line 1058

def initialize(s, x, y, args = {})
  raise "No canvas" unless @@pipe
  @name = TkObject.nextId
  @coords = [ x, y ]
  @penpoint = nil
  args[:anchor] = :nw unless args.has_key?(:anchor)
  args[:text] = "\"#{s}\""
  cmnd = "set #{@name} [.canvas create text #{@coords.join(" ")} #{TkObject.options(args)}]"
  @@pipe.puts cmnd
end

Instance Method Details

#update(s) ⇒ Object

Replace the string displayed by a text object. Example: erase the current string for object x and replace it with “hello, world”:

>> x.update("hello, world")
=> nil

The new string is displayed at the same location and with the same attributes as the old string.



1076
1077
1078
# File 'lib/rubylabs.rb', line 1076

def update(s)
  @@pipe.puts ".canvas itemconfigure $#{name} -text \"#{s}\""
end