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 = {})


1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
# File 'lib/rubylabs.rb', line 1065

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.



1083
1084
1085
# File 'lib/rubylabs.rb', line 1083

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