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

scrollbar event handling

16 views
Skip to first unread message

Paul

unread,
Jan 5, 2012, 3:20:15 PM1/5/12
to
Hi,

I've got a ttk::scrollbar in an application set with horizontal
orientation. What I want to do is bind an event for when the left
arrow button is clicked, bind another event for when the right arrow
button is clicked, and a finally bind an event for when the bar is
dragged.

I don't want to connect the scrollbar to another component at the
moment. Can this be done? The only examples I've found so far are for
scrollbars connected to other components.

Eventually the scollbar will control another component, but it won't
be a standard tk component.

Thanks,

Paul.

Aric Bills

unread,
Jan 5, 2012, 5:39:25 PM1/5/12
to
You can accomplish your goal using the scrollbar's -command option.
Whenever the user interacts with the scrollbar, the command you
specified will be called. Additional arguments will be appended
depending on how the scrollbar was used:

* if the left button was clicked, the appended arguments are: scroll
-1 units
* if the right button was clicked: scroll 1 units
* if the left trough was clicked: scroll -1 pages
* if the right trough was clicked: scroll 1 pages
* if the slider was dragged: moveto $location (where $location is a
real number between 0 and 1)

When you respond to scrollbar events, you also need to reposition the
scrollbar to reflect whatever changes have happened. Do this by
calling [$scrollbar set]. Scrollable widgets have -xscrollcommand and -
yscrollcommand options for this purpose; for example, if $text is a
text widget and $yscroll is a vertical scrollbar, your program might
include the line

$text configure -yscrollcommand [list $yscroll set]

When you write the API for your non-standard component, consider
making it as standard as possible, including an -xscrollcommand option
to handle setting the scrollbar and an xview method to respond to
scrollbar events. You might find the snit package helpful for this.

Alexandre Ferrieux

unread,
Jan 5, 2012, 5:42:27 PM1/5/12
to
On Jan 5, 9:20 pm, Paul <pault...@googlemail.com> wrote:
> Hi,
>
> I've got a ttk::scrollbar in an application set with horizontal
> orientation. What I want to do is bind an event for when the left
> arrow button is clicked, bind another event for when the right arrow
> button is clicked, and a finally bind an event for when the bar is
> dragged.
>
> I don't want to connect the scrollbar to another component at the
> moment. Can this be done? The only examples I've found so far are for
> scrollbars connected to other components.

Look at the manpage: the -command option and "set" widget command are
the scrollbar's only interface to whatever it controls. It doesn't
even know whether it is another widget or a 100-ton crane.

Try this:

array set speed {units 0.1 pages 0.2}

proc foo args {
puts FOO:$args
foreach {a b c} $args break
switch $a {
moveto {.s set $b [expr {$b+0.1}]}
scroll {
set cur [lindex [.s get] 0]
foo moveto [expr {$cur+$b*$::speed($c)}]
}
}
}

ttk::scrollbar .s -command foo
.s set 0.4 0.5
place .s -x 0 -y 0 -width 20 -height 200
. configure -width 20 -height 200


This example is very crude, and has none of the usual boundary checks
(try approaching the ends). This is to show you how completely free
the scrollbar is of any assumption about the controlled thing.

As a by-product, the first argument to foo (along with the third for
'scroll') allows you to know which element was clicked.

HTH,

-Alex

Paul

unread,
Jan 6, 2012, 1:02:58 PM1/6/12
to
On Jan 5, 10:42 pm, Alexandre Ferrieux <alexandre.ferri...@gmail.com>
wrote:
Aric/Alex, thanks very much for the responses, my code is now working
nicely.

Paul.
0 new messages