How to pass an object from inside its own method to a helper function

Liste des GroupesRevenir à cl tcl 
Sujet : How to pass an object from inside its own method to a helper function
De : mark (at) *nospam* qtrac.eu (Mark Summerfield)
Groupes : comp.lang.tcl
Date : 11. Jul 2024, 10:44:07
Autres entêtes
Message-ID : <mwSdnbvrgID6NBL7nZ2dnZfqnPednZ2d@brightview.co.uk>
User-Agent : Pan/0.154 (Izium; 517acf4)
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)


Date Sujet#  Auteur
11 Jul 24 * How to pass an object from inside its own method to a helper function5Mark Summerfield
11 Jul 24 `* Re: How to pass an object from inside its own method to a helper function4Mark Summerfield
11 Jul 24  `* Re: How to pass an object from inside its own method to a helper function3Schelte
11 Jul 24   `* Re: How to pass an object from inside its own method to a helper function2Mark Summerfield
11 Jul 24    `- Re: How to pass an object from inside its own method to a helper function1Ralf Fassel

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal