Deutsch   English   Français   Italiano  
<mwSdnbvrgID6NBL7nZ2dnZfqnPednZ2d@brightview.co.uk>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!local-1.nntp.ord.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 11 Jul 2024 09:44:07 +0000
From: Mark Summerfield <mark@qtrac.eu>
Subject: How to pass an object from inside its own method to a helper function
Newsgroups: comp.lang.tcl
MIME-Version: 1.0
User-Agent: Pan/0.154 (Izium; 517acf4)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <mwSdnbvrgID6NBL7nZ2dnZfqnPednZ2d@brightview.co.uk>
Date: Thu, 11 Jul 2024 09:44:07 +0000
Lines: 50
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-fvsTsUFLGCbiVpMnctRxs0+yvre6XQyqIKT89d/e9FnxDIHahqrz+N6T6R5Mz3AipPOPQhD/NVHQcZS!wHgs1bEd5uKQqhBQsR2ZVGHLmsw16Uwu3Utb2ZauW1doTvwCgFYj4Wo8EIC51Vg3NfVgYmEjAjkt!FPjCdPG+tx1Y8H/+40Z+lVm/dg==
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
Bytes: 2469

I want to pass an object from inside its own method to a helper function. 
(I'd also like to know how to pass a bound method.)

In the example below I have tried using [self] and [self object] and [self 
namespace] but none of them works.

#!/usr/bin/env wish9
tk appname "Test App"
proc make_file_menu {app} {
    .menu.file add command -command {$app on_quit} -label Quit \
            -underline 0 -accelerator Ctrl+Q
}
oo::class create App {
    constructor {} {
        wm withdraw .
        wm title . [tk appname]
        grid [ttk::button .quitButton -text Quit -underline 0 \
              -command [callback on_quit]]
        bind . <Escape> [callback on_quit]
        bind . <Alt-q> [callback on_quit]
        menu .menu
        menu .menu.file
        .menu add cascade -menu .menu.file -label File -underline 0
        make_file_menu [self] ;# BUG what do I pass here as "this"
        . configure -menu .menu
    }
    method on_quit {} {destroy .}
    method show {} {
        wm deiconify .
        raise .
    }
}
set application [App new]
$application show

The error I get is:

can't read "app": no such variable
    while executing
"$app on_quit"
    invoked from within
".#menu.#menu#file invoke active"
    ("uplevel" body line 1)
    invoked from within
"uplevel #0 [list $w invoke active]"
    (procedure "tk::MenuInvoke" line 49)
    invoked from within
"tk::MenuInvoke .#menu.#menu#file 1"
    (command bound to event)