Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POO, Itcl and Tk 1

Status
Not open for further replies.

Anto35

Technical User
May 16, 2002
25
FR
Good evening,

I use Itcl, but I've got many problem with it!
I'd like to know how can we access to $this or to a class variable in the '-command' option of a Tk button.

I've got another problem, when I create two objects car1 and car2 which belong to the same Itcl class, and when I acces to an Itcl variable of car1, from a C procedure using Tcl_GetVar(...), the result is not always the value of the car1's variable! The result is often the value of another object's variable, car2's variable...

I'm very Angry, could you help me before my computer fall down...
Thanx!

 
Well, now I just need to call an ITCL method when a button is activate!
I try to put the call to the function in the "-command" option of a button but it doesn't work!

.butt m -font ... -text ... -command {$this myMethod Arg1...}

So I remplaced $this by the object's name (Uh...), it works but my button is invoked by many objects so I had to use $this or a stuff like this

Thankx for your answers!
Ciao...
 
Using [ignore][incr Tcl][/ignore] objects with callbacks is one of the trickier aspects of working with [ignore][incr Tcl][/ignore]. To give a bit more context to those who don't have a lot of experience with this topic, callbacks are Tcl code executed in response to an event, such as:[ul][li]Window events (e.g., bind actions, -command actions, etc.)[/li][li]File events (registered with the fileevent command)[/li][li]Timer events (registered with the after command)[/li][/ul]By default, callbacks execute in the global scope of the global namespace.

The ::itcl::code command captures the object context for a code fragment:

[tt]::itcl::code command ?arg...?[/tt]

The return value is a code fragment that can be executed outside of context of the object, while still having access to all object methods and data members (including private methods and data members).

So, the way to register your Itcl method as a callback looks something like this:

Code:
button .b –text "Ok" –command   [::itcl::code $this privateMethod arg1 arg2]

You face a similar problem when using widget options such as -variable, -textvariable, etc. with object data members. The ::itcl::scope command returns a fully-qualified reference to a data member, even a private one, that can be used outside of the context of an object:

[tt]::itcl::scope varName[/tt]

Here's a simple example demonstrating the use of both ::itcl::code and ::itcl::scope. It's a class called Toggle, which creates a checkbutton and a button. Toggling the checkbutton enables and disables the button:

Code:
package require Itcl

::itcl::class Toggle {
  private variable _state "normal"
    
  constructor {} {
    checkbutton .cb -text Enable       -variable [::itcl::scope _state]       -onvalue "normal" -offvalue "disabled"       -command [::itcl::code $this toggle]
    .cb select  ;# The checkbutton is initially on
        
    button .b -text "Ok"
        
    pack .cb .b -padx 4 -pady 4
  }
    
  private method toggle {} {
      .b configure -state $_state
  }
}

Toggle t
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top