Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Implementing swipe event over multiple checkbuttons

41 views
Skip to first unread message

Alexandru

unread,
Mar 26, 2021, 11:17:59 AM3/26/21
to
Hi,

I'm trying to implement a new behavior for checkbuttons:
If the user swipes with the pressed left mouse button over multiple checkbuttons, then the states of the checkbuttons should be changed.
This would be much faser then single clicks on every checkbutton.

My code is appended and it's not working. Looks like the binding to <Enter> is not working if the mouse button is already pressed. Is there another way for this?

Thanks!
Alexandru

destroy .test
toplevel .test
pack [frame .test.f] -side left -expand 1 -fill both
pack [ttk::checkbutton .test.f.b1 -style Toolbutton -text test] -side right -expand 0 -fill x
pack [ttk::checkbutton .test.f.b2 -style Toolbutton -text test] -side right -expand 0 -fill x
pack [ttk::checkbutton .test.f.b3 -style Toolbutton -text test] -side right -expand 0 -fill x
event add <<Swipe>> <ButtonPress-1><Motion>
bind .test.f <<Swipe>> {::SwipeCallback %W}
bind .test.f <<Swipe>> {::SwipeCallback %W}
bind .test.f.b1 <Enter> {::EnterCallback %W}
bind .test.f.b2 <Enter> {::EnterCallback %W}
bind .test.f.b3 <Enter> {::EnterCallback %W}
proc ::SwipeCallback {args} {
puts "swipe $args"
return 1
}
proc ::EnterCallback {args} {
puts "enter $args"
return 1
}

nemethi

unread,
Mar 26, 2021, 12:10:59 PM3/26/21
to
Am 26.03.21 um 16:17 schrieb Alexandru:
Change the code starting with "event add" to become:

event add <<Swipe>> <B1-Motion>
foreach w {.test.f .test.f.b1 .test.f.b2 .test.f.b3} {
bind $w <<Swipe>> {::SwipeCallback %W %X %Y}
}
proc ::SwipeCallback {w rootX rootY} {
puts [winfo containing $rootX $rootY]
}

--
Csaba Nemethi https://www.nemethi.de mailto:csaba....@t-online.de

Alexandru

unread,
Mar 26, 2021, 1:14:49 PM3/26/21
to
Cool! It works like a charm.
Many thanks, Csaba.
Cheers
Alex

Alexandru

unread,
Mar 27, 2021, 1:43:22 AM3/27/21
to
My code now runs as expected. I had to add a global variable to check if the callback is invoked on the same checkbutton twice in a row.
Code below.
There is one last question remaining.
If the contaning frame has no space arround the checkbuttons, the <<Swipe>> event cannot be executed. Somehow, the binding does not work on the checkbutton itself.
So either I find a way to propagate the event from the button to the containing frame, or I add another binding to the buttons, or I must make sure the frame has some extra empty space for the clicks.

set ::lastcheckbutton ""
destroy .test
toplevel .test
pack [frame .test.f] -side left -expand 1 -fill both
pack [ttk::checkbutton .test.f.b1 -style Toolbutton -text test] -side right -expand 0 -fill x
pack [ttk::checkbutton .test.f.b2 -style Toolbutton -text test] -side right -expand 0 -fill x
pack [ttk::checkbutton .test.f.b3 -style Toolbutton -text test] -side right -expand 0 -fill x
event add <<Swipe>> <B1-Motion>
foreach w {.test.f} {
bind $w <<Swipe>> {::SwipeCallback %X %Y}
}
proc ::SwipeCallback {rootX rootY} {
variable lastcheckbutton
set w [winfo containing $rootX $rootY]
if {$w==""} {
return
}
if {[winfo class $w]=="TCheckbutton" && $lastcheckbutton!=$w} {
$w invoke
set lastcheckbutton $w
}
}

Alexandru

unread,
Mar 27, 2021, 2:26:28 AM3/27/21
to
I got it working now by using "bindtags":

proc ::SwipeInit {parentframe args} {
bind $parentframe <B1-Motion> {::SwipeCallback %X %Y}
foreach child $args {
bindtags $parentframe.$child [list $parentframe all TCheckbutton]
}
}
proc ::SwipeCallback {rootX rootY} {
variable lastcheckbutton
set w [winfo containing $rootX $rootY]
if {$w==""} {
return -code continue
}
if {![info exists lastcheckbutton]} {
set lastcheckbutton ""
}
if {[winfo class $w]=="TCheckbutton" && $lastcheckbutton!=$w} {
$w invoke
set lastcheckbutton $w
}
return -code continue
}

destroy .test
toplevel .test
pack [frame .test.f] -side left -expand 1 -fill both
pack [ttk::checkbutton .test.f.b1 -style Toolbutton -text test] -side right -expand 0 -fill x
pack [ttk::checkbutton .test.f.b2 -style Toolbutton -text test] -side right -expand 0 -fill x
pack [ttk::checkbutton .test.f.b3 -style Toolbutton -text test] -side right -expand 0 -fill x
::SwipeInit .test.f b1 b2 b3
0 new messages