• A simple (?) Tk question

    From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Wed Apr 17 16:21:02 2024
    From Newsgroup: comp.lang.tcl

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Wed Apr 17 16:28:27 2024
    From Newsgroup: comp.lang.tcl

    * Helmut Giese <hgiese@ratiosoft.com>
    | 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'
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Thu Apr 18 15:00:16 2024
    From Newsgroup: comp.lang.tcl

    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
    ...
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Gerald Lester@Gerald.Lester@gmail.com to comp.lang.tcl on Thu Apr 18 08:07:16 2024
    From Newsgroup: comp.lang.tcl

    On 4/17/24 09:21, 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

    Try checkbutton
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Fri Apr 19 11:34:28 2024
    From Newsgroup: comp.lang.tcl

    * Helmut Giese <hgiese@ratiosoft.com>
    | 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'
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From et99@et99@rocketship1.me to comp.lang.tcl on Fri Apr 19 17:24:28 2024
    From Newsgroup: comp.lang.tcl

    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

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Andreas Leitgeb@avl@logic.at to comp.lang.tcl on Sat Apr 20 22:05:11 2024
    From Newsgroup: comp.lang.tcl

    et99 <et99@rocketship1.me> 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".

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Emiliano@emil.gavilan@gmail.com to comp.lang.tcl on Sat Apr 20 19:21:33 2024
    From Newsgroup: comp.lang.tcl

    On Wed, 17 Apr 2024 16:21:02 +0200
    Helmut Giese <hgiese@ratiosoft.com> 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
    Are you looking for a checkbutton instead? In this case is simply
    pack [checkbutton .btn -width 3 -text "" -indicatoron 0 -variable .btnState]
    With ttk you can get the same effect with
    pack [ttk::checkbutton .btn -width 3 -text "" -style Toolbutton -variable .btnState]
    Regards
    --
    Emiliano
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Sun Apr 21 14:43:52 2024
    From Newsgroup: comp.lang.tcl

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114