# Tk and instance variables (pickaxe book Ed 2, p260)

**URL:** <https://www.ruby-forum.com/t/tk-and-instance-variables-pickaxe-book-ed-2-p260/61466>\
**Category:** Ruby\
**Created:** [January 14, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/tk-and-instance-variables-pickaxe-book-ed-2-p260/61466 "2006-01-14T22:56:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 14, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/tk-and-instance-variables-pickaxe-book-ed-2-p260/61466/1 "2006-01-14T22:56:18Z")

</div>

Chaps,

There’s a small section of code from the second edition of the pickaxe  
book on p260.

require ‘tk’

image1 = TkPhotoImage.new {file “img1.gif” }  
image2 = TkPhotoImage.new {file “img2.gif” }

b = TkButton.new(@root) do # \<---- This line

# more code

# not shown for sake of brevity

end

# more code

# more code

Am I right in saying that the line that creates the new button has an  
instance variable that will be used to define the main root “container”?

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/tk-and-instance-variables-pickaxe-book-ed-2-p260/61466/2 "2006-01-21T15:40:04Z")

</div>

On Sat, 14 Jan 2006 22:54:58 +0100, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> b = TkButton.new(@root) do # \<---- This line
> 
> # more code
> 
> # not shown for sake of brevity
> 
> end
> 
> # more code
> 
> # more code
> 
> Am I right in saying that the line that creates the new button has an  
> instance variable that will be used to define the main root “container”?

If my (horrible) memory of the 15 minutes I’ve worked in Tk serves me  
right for a change, there’s a

@root = TkRoot.new

missing there somewhere. All other widgets should require a parent  
container parameter for their constructor. TkRoot serves as the toplevel  
container for a Tk interface and contains some plumbing related to the  
event loop and such.

David V.
