Re: My hang-up about OOP (snit)

Liste des GroupesRevenir à cl tcl 
Sujet : Re: My hang-up about OOP (snit)
De : heller (at) *nospam* deepsoft.com (Robert Heller)
Groupes : comp.lang.tcl
Date : 28. Jun 2024, 03:24:19
Autres entêtes
Organisation : Deepwoods Software
Message-ID : <zv6dnSg8IeFOg-P7nZ2dnZfqn_ednZ2d@giganews.com>
References : 1 2
User-Agent : TkNews 3.0 (1.2.18)
At Thu, 27 Jun 2024 21:27:04 -0300 Luc <luc@sep.invalid> wrote:

 
On Thu, 27 Jun 2024 23:41:16 +0000, Robert Heller wrote:
 
Snit always seemed to have good enough documentation for me.
 
 
Of course I am not very bright (or I have no previous experience with
OOP if you want to be kind), but I am struggling with using snit at all
because of this:
 
The world famous snit faq says:
 
"For example, the following code creates a read-only text widget... "
etc. etc.
 
::snit::widgetadaptor rotext {
 
    constructor {args} {
        # Create the text widget; turn off its insert cursor
        installhull using text -insertwidth 0
 
        # Apply any options passed at creation time.
        $self configurelist $args
    }
 
    # Disable the text widget's insert and delete methods, to
    # make this readonly.
    method insert {args} {}
    method delete {args} {}
 
    # Enable ins and del as synonyms, so the program can insert and
    # delete.
    delegate method ins to hull as insert
    delegate method del to hull as delete
   
    # Pass all other methods and options to the real text widget, so
    # that the remaining behavior is as expected.
    delegate method * to hull
    delegate option * to hull
}
 
 
Yeah, the document has some examples.
Now, how do I call that? I did it like this:
 
rotext .rox
 
and nothing happened. Well, I got the Tk gray box.

The snit::widget and snit::widgetadaptor entities work *exactly* like "native"
Tk widgets.

For a regular text widget you would do:

test .t
pack .t -fill both -expand yes

so for a rotext, you would do:

rotext .rox
pack .rox -fill both -expand yes

The widget constructor doesn't 'automatically' pack the widget.  Although pack
is probably the most common geometry manager, you could use grid or place, so
the choice of geometry manager (and its options) is left up to the programmer.


 
The world famous snit faq says:
 
"For example, the following code creates a read-only text widget... "
 
Where is the text widget then?
 
After a lot of struggle, I fixed it by adding 'pack $win' within the
constructor. Why can't the manual tell me that? Why can't the example
code include it?

*DON'T* put the pack in the constructor!  One of the common ways to create a
Scroll Window (a frame with scroll bars intended to contain a scrollable
widget (like a text or canvas or listbox, etc.) uses grid and if the text is
already packed, this will fail with an error.

ALL Tk widget constructors leave the geometry manager (pack, grid, or place)
out. It is the responsibility of the programmer to add the appropriate
geometry manager code after creating the widget. This is always how this is
done. The geometry managers have many options controlling how the widget's
geometry is managed and none of these make sense as options to the widget
creation. There are also cases where the widget might not get set up with a
geometry manager right away or might get disassociated with a geometry manager
at some point (eg pack forget).  Geometry management is a whole separate
process, and is always handled separately from widget creation.

 
 
There was more struggle.
 
snit::widget TopWindow {
    hulltype toplevel
    delegate option * to hull
    component of
    variable of
 
    constructor {args} {
wm withdraw .
wm resizable $win 1 1
wm attributes $win -zoomed 0
wm geometry $win 600x100+0+0
wm title $win "test"
tk appname "test"
bind $win <Alt-q> {exit 0}
bind $win <Alt-Q> {exit 0}

install of using frame $win.of
pack $of -fill both -expand 1
 
        $self configurelist $args
}
}
TopWindow .toplevel
 
 
Now, how do I add the read-only text widget to that toplevel?
 
Again, a lot of struggle to figure this out:
 
rotext .toplevel.of.rox
 
Why can't the manual tell me that?

Did you read the man pages for *Tk* itself?

snit::widget and snit::widgetadaptor create widget constructors that behave
*EXACTLY* like native Tk widgets.  All of the things talked about in the man
pages for Tk and all of the native Tk widgets and Tk geometry managers apply
to widgets created with snit::widget and snit::widgetadaptor apply. 
snit::widget and snit::widgetadaptor create "commands" that can be dropped
into anyplace a any native Tk widget creation command would be at home.

snit::widgetadaptor MySpecialButton {
..
}

then

set B01 [MySpecialButton .b ...]
pack $B01

*JUST LIKE*

set B01 [button .b ...]
pack $B01

*OR*

set B01 [ttk::button .b ...]
pack $B01

 
And I can only do that by explicitly referencing ".toplevel"
which seems inadequate. There should be a variable that contains
that toplevel path. There probably is, but heck if I know what
that is.

set mytoplevel [TopLevel .toplevel]

*EXACTLY* like:

set mytoplevel [toplevel .toplevel]

 
I'm still struggling with other things. Anyway, my point is
the documentation has multiple examples of how to create a
type but no examples whatsoever of how to call what the code
is creating. It just assumes the reader knows because it's obvious.
But it took me a long time to guess on my own.

That is because it is already documented elsewhere in the Tk man pages.

 
 
Thank you for your guidance once again.
 

--
Robert Heller             -- Cell: 413-658-7953 GV: 978-633-5364
Deepwoods Software        -- Custom Software Services
http://www.deepsoft.com/  -- Linux Administration Services
heller@deepsoft.com       -- Webhosting Services
                                                                                                                       

Date Sujet#  Auteur
25 Jun 24 * My hang-up about OOP (snit)22Luc
25 Jun 24 +* Re: My hang-up about OOP (snit)10Rich
25 Jun 24 i`* Re: My hang-up about OOP (snit)9Luc
26 Jun 24 i +- Re: My hang-up about OOP (snit)1Robert Heller
26 Jun 24 i +- Re: My hang-up about OOP (snit)1Rich
26 Jun 24 i +- Re: My hang-up about OOP (snit)1Rich
26 Jun 24 i `* Re: My hang-up about OOP (snit)5Rich
27 Jun 24 i  +* Re: My hang-up about OOP (snit)3Luc
27 Jun 24 i  i`* Re: My hang-up about OOP (snit)2Rich
27 Jun 24 i  i `- Re: My hang-up about OOP (snit)1Lawrence Woodman
27 Jun 24 i  `- Re: My hang-up about OOP (snit)1Robert Heller
26 Jun 24 +- Re: My hang-up about OOP (snit)1saito
26 Jun 24 +* Re: My hang-up about OOP (snit)9Robert Heller
27 Jun 24 i`* Re: My hang-up about OOP (snit)8Luc
27 Jun 24 i `* Re: My hang-up about OOP (snit)7Robert Heller
27 Jun 24 i  `* Re: My hang-up about OOP (snit)6Luc
28 Jun 24 i   `* Re: My hang-up about OOP (snit)5Robert Heller
28 Jun 24 i    `* Re: My hang-up about OOP (snit)4Luc
28 Jun 24 i     `* Re: My hang-up about OOP (snit)3Robert Heller
28 Jun 24 i      `* Re: My hang-up about OOP (snit)2Luc
28 Jun 24 i       `- Re: My hang-up about OOP (snit)1Robert Heller
26 Jun 24 `- Re: My hang-up about OOP (snit)1Luis Mendes

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal