nitep70 has asked for the wisdom of the Perl Monks concerning the following question:

Here is a tiny example of a large program in which a scrollable array is updated by the program. Users can remove entries, change entries and move entries which all works nicely. (Not shown here) However when the program adds new entries at the end of the list and the list is longer than the viewable window, then the last entry is not shown. How can I get the program to position the cursor so that the last addition can be seen without the user having to move the cursor in the scroll-bar? Thanks

use strict; use Tkx; my $mw = Tkx::widget->new("."); $mw->g_wm_title("test"); my @results=(); my @id6=(); for my $i(0..100) {$results[$i] = 2*$i;} &do_canvas4; Tkx::MainLoop(); sub do_canvas4 {my $canvas4 = $mw->new_tk__canvas(-width => 130, -height => 500,-scro +llregion => "0 4 0 2500"); $canvas4->g_grid(-column => 2, -row => 4, -sticky => "nsew"); my $vscroll = $mw->new_tk__scrollbar(-orient => "vertical", -command = +> [$canvas4, "yview"]); $vscroll->g_grid(-column => 3, -row => 4, -sticky => "nsew"); $mw->new_ttk__sizegrip()->g_grid(-column => 3, -row => 4, -sticky => " +e"); $canvas4->configure(-yscrollcommand => [$vscroll, "set"]); for my $i(0..60) { $id6[$i] = $canvas4->create_text(79, 22+$i*26,-text => $results[$i +],-font => "fixed 16 bold",-activefill => "red",-fill => "black",-anc +hor => "e"); # $canvas4->bind($id6[$i], '<1>' => sub {handle_click3($id6[$i],$i) +}); plus also middle and right button bindings in the program } }

Replies are listed 'Best First'.
Re: Tkx scrolling
by kcott (Archbishop) on Sep 27, 2017 at 21:58 UTC

    G'day nitep70,

    Welcome to the Monastery.

    I find the autoscroll package handles all types of scrolling cleanly. The basic Tkx code looks like this:

    Tkx::package_require('autoscroll'); ... # Automatically mapped scrollbar Tkx::__autoscroll__autoscroll($sb); ... # Fixed scrollbar Tkx::__autoscroll__unautoscroll($sb);

    There's a CPAN module, Tkx::Scrolled, which emulates this. You might like to look at its source code to get a few ideas. Be aware, by its own admission: "... we kludge it ..." (see its comments before the '_set' subroutine).

    In the code you've posted, some of your g_grid() option values look dodgy. A vertical scrollbar should be "-sticky => 'ns'": you have "-sticky => 'nsew'". Your scrollbar and sizegrip both have "-column => 3, -row => 4": that doesn't look right.

    I'm also wondering about text in a canvas widget: a listbox widget seems more appropriate for the scenario you describe.

    If you tidy up your code, it will be easier for you to read and less prone to errors. It wasn't a pleasure for me to read: I may have missed things. You can get some hints from perlstyle; also see perltidy — the choice of code layout is yours but, when you have chosen, use it consistently.

    There may be other areas that could be improved; however, this code at the start stood out:

    my @results=(); my @id6=(); for my $i(0..100) {$results[$i] = 2*$i;} &do_canvas4;

    I probably would have written that more like:

    my @results = map 2*$_, 0 .. 100; my @id6; do_canvas4();

    Using '&subname' is generally not what you want; use 'subname()' unless you really know why you're not. See perlsub for more on that.

    — Ken

      Thanks for your answer.

      I have since found that

      $canvas4->yview('moveto', (.09)); # where a fraction 0 to 1 is used t +o position the cursor

      works nicely to do what I want.

      I write in linux but the sailing club I write for uses ms windows so I compile it in XP in Virtualbox where the version of Perl requires &subname()

      Also for my $i(0..100) {$results[$i] = 2*$i;} is not in the original code. That was just to use in the small demo.

      See https://www.youtube.com/watch?v=ezNxXLDAPVY if interested.

      2017-10-07 Athanasius added code tags and linkified the web address

        "... ms windows ... XP in Virtualbox where the version of Perl requires &subname()"

        I don't see how WinXP has any bearing on this. I have a very old laptop, that I probably only use a couple of times a year: it runs WinXP and has Perl installed. I've never had any reason to use '&subname()' for any Perl code on that machine.

        I don't see the relevance of mentioning VirtualBox. I've certainly run Perl code under VirtualBox/WinXP for cross-platform testing purposes in the past: again, I've never had any reason to use '&subname()'.

        What version of Perl are you running that you think requires '&subname()'?

        If you run the following, do you get the the same result that I show?

        $ perl -le 'sub test { 42 } print test(); print &test()' 42 42

        — Ken

        A reply falls below the community's threshold of quality. You may see it by logging in.