• Re: A simple (?) Tk question

    From Ralf Fassel@21:1/5 to All on Wed Apr 17 16:28:27 2024
    * Helmut Giese <[email protected]>
    | Hello out there,
    | for a kinf´d of technical app I need a 'switch type' button - that is
    | a button stays pressed when pressed once and only gets released when
    | pressed again. (Whereas the normal GUI behaviour of buttons is more
    | the 'door bell' type).
    --<snip-snip>--
    | Any help will be greatly appreciated

    Check the -indicatoron option for TCL *radiobuttons*.

    Command-Line Name:-indicatoron
    Database Name: indicatorOn
    Database Class: IndicatorOn

    Specifies whether or not the indicator should be drawn. Must be
    a proper boolean value. If false, the -relief option is ignored
    and the widget's relief is always sunken if the widget is se-
    lected and raised otherwise.

    HTH
    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Giese@21:1/5 to All on Wed Apr 17 16:21:02 2024
    Hello out there,
    for a kinf�d of technical app I need a 'switch type' button - that is
    a button stays pressed when pressed once and only gets released when
    pressed again. (Whereas the normal GUI behaviour of buttons is more
    the 'door bell' type).
    I did come as far as keeping it pressed using the code below:
    ---
    package require Tk
    foreach ch [winfo children "."] {destroy $ch}

    proc btnPress {w var} {
    if {[set $var]} {
    puts "calling 'tk::ButtonUp $w'"
    tk::ButtonUp $w
    set $var 0
    } else {
    puts "calling 'tk::ButtonDown $w'"
    tk::ButtonDown $w
    set $var 1
    }
    }

    proc tst {} {
    set btn [button .btn -width 3 -text ""]
    pack $btn
    set ::${btn}State 0
    bind $btn <Button-1> [list btnPress %W ::${btn}State]
    bind $btn <Enter> {break}
    bind $btn <Leave> {break}
    bind $btn <<Invoke>> {break}
    bind $btn <Key-space> {break}
    bind $btn <ButtonRelease-1> {break}
    return $btn
    }

    set b [tst]
    ---
    All the 'break' statements are for disabling the class bindings for
    'Button'.
    What I see is
    - on the first press the ButtonDown message appears and the button
    gets pressed
    - on following presses I see the Up/Down messages appear as they
    should - but my button never gets released again.
    This is on Windows 10 and Tcl 8.6.10.

    Any help will be greatly appreciated
    Helmut

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Giese@21:1/5 to All on Thu Apr 18 15:00:16 2024
    Hello Ralf,

    Check the -indicatoron option for TCL *radiobuttons*.

    Command-Line Name:-indicatoron
    Database Name: indicatorOn
    Database Class: IndicatorOn

    Specifies whether or not the indicator should be drawn. Must be
    a proper boolean value. If false, the -relief option is ignored
    and the widget's relief is always sunken if the widget is se-
    lected and raised otherwise.

    this is interesting. I will see, what it looks like.
    Many thanks
    Helmut
    PS: But I still would like to know why that button doesn't go up again
    ...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Fri Apr 19 11:34:28 2024
    * Helmut Giese <[email protected]>
    | PS: But I still would like to know why that button doesn't go up again

    I *think* you need to 'break' in the Button-1 binding as well, since
    otherwise the class binding will fire as well and turn the button to
    'Pressed' again.

    Rather than 'break'-ing in each and every binding you could just remove the 'Button' class bindings via 'bindtag' from that single button:

    proc tst {} {
    set btn [button .btn -width 3 -text ""]
    pack $btn
    # !!!
    bindtags $btn [list $btn . all]
    set ::${btn}State 0
    bind $btn <Button-1> [list btnPress %W ::${btn}State]
    return $btn
    }

    HTH
    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From et99@21:1/5 to Helmut Giese on Fri Apr 19 17:24:28 2024
    On 4/17/2024 7:21 AM, Helmut Giese wrote:
    Hello out there,
    for a kinf´d of technical app I need a 'switch type' button - that is
    a button stays pressed when pressed once and only gets released when
    pressed again. (Whereas the normal GUI behaviour of buttons is more
    the 'door bell' type).
    I did come as far as keeping it pressed using the code below:
    ---
    package require Tk
    foreach ch [winfo children "."] {destroy $ch}

    proc btnPress {w var} {
    if {[set $var]} {
    puts "calling 'tk::ButtonUp $w'"
    tk::ButtonUp $w
    set $var 0
    } else {
    puts "calling 'tk::ButtonDown $w'"
    tk::ButtonDown $w
    set $var 1
    }
    }

    proc tst {} {
    set btn [button .btn -width 3 -text ""]
    pack $btn
    set ::${btn}State 0
    bind $btn <Button-1> [list btnPress %W ::${btn}State]
    bind $btn <Enter> {break}
    bind $btn <Leave> {break}
    bind $btn <<Invoke>> {break}
    bind $btn <Key-space> {break}
    bind $btn <ButtonRelease-1> {break}
    return $btn
    }

    set b [tst]
    ---
    All the 'break' statements are for disabling the class bindings for
    'Button'.
    What I see is
    - on the first press the ButtonDown message appears and the button
    gets pressed
    - on following presses I see the Up/Down messages appear as they
    should - but my button never gets released again.
    This is on Windows 10 and Tcl 8.6.10.

    Any help will be greatly appreciated
    Helmut


    You could use a label and toggle the relief between raised and sunken.

    set lstate 1
    label .label -text hello -relief raised
    bind .label <1> {toggleit}
    proc toggleit {} {
    if {[set ::lstate [expr {1-$::lstate}]]} {
    .label configure -relief raised
    } else {
    .label configure -relief sunken
    }
    }
    pack .label



    -e

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Leitgeb@21:1/5 to [email protected] on Sat Apr 20 22:05:11 2024
    et99 <[email protected]> wrote:
    On 4/17/2024 7:21 AM, Helmut Giese wrote:
    Hello out there,
    for a kinf´d of technical app I need a 'switch type' button - that is
    a button stays pressed when pressed once and only gets released when
    pressed again. (Whereas the normal GUI behaviour of buttons is more
    the 'door bell' type).

    It was answered already, but might have been missed...


    The answer is to use "checkbutton" instead of "button" or "radiobutton".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Giese@21:1/5 to All on Sun Apr 21 14:43:52 2024
    Hello,
    many thanks to all who responded.
    I didn't know it was that easy.
    For me a checkbutton always was this square where one could put a
    check mark in - hence the name. It never occurred to me that it could
    look totally different.
    Seems like there is still a lot to learn ...
    Thanks again
    Helmut

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)