免费注册 查看新帖 |

ChinaUnix.net

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2347 | 回复: 2

Tk中用Canvas绘图,Scrollbar不起作用 [复制链接]

论坛徽章:
0
发表于 2018-02-08 21:51 |显示全部楼层
下面代码,Scrollbar出现,但不起作用,请教:
  1. <p>use Tk;
  2. use utf8;
  3. $mw = MainWindow->new( );
  4. $mw->geometry("300x100");#1368
  5. $mw->title("模式关系网");#
  6. $mw->title("One Scrollbar/Three Listboxes");
  7. $mw->Button(-text => "Exit",
  8.             -command => sub { exit })->pack(-side => 'bottom');

  9. $scltest = $mw->Scrollbar( );

  10. $canvas =  $mw->Canvas();


  11. sub scroll_canvas {
  12.   my ($scltest, $canvas,  @args) = @_;
  13.   $scltest->set(@args);

  14. }


  15. $canvas->configure(-yscrollcommand => [ \&scroll_canvas, $scltest,
  16.                                        $canvas, ]);


  17. # Configure the Scrollbar to scroll each Listbox
  18. $scltest->configure(-command => ['yview' => $canvas]);

  19. # Pack the Scrollbar and Listboxes

  20. $scltest->pack(-side => 'left', -fill => 'y');

  21. $canvas->pack(-side => 'left');

  22. my $bt102 = $canvas->Button(-text => "下个方法",
  23.                         -command => \&printinfo);

  24. my $id102 = $canvas->createWindow(36,30, -window =>$bt102);

  25. my $bt103 = $canvas->Button(-text => "上个方法",
  26.                         -command => \&printinfo);


  27. my $id104 = $canvas->createWindow(36,200, -window =>$bt104);


  28. my $bt105 = $canvas->createLine( 30,40,400,400, -fill =>  "green"
  29.                         );

  30. MainLoop;

  31. sub printinfo
  32. {
  33.          print "ok!\n";
  34. }</p><p>
  35. </p><p>但是下面的代码是有效的:</p><p>
  36. </p><p>use Tk;</p><p>$mw = MainWindow->new( );
  37. $mw->geometry("200x200");#1368
  38. $mw->title("模式关系网");#
  39. $mw->title("One Scrollbar/Three Listboxes");
  40. $mw->Button(-text => "Exit",
  41.             -command => sub { exit })->pack(-side => 'bottom');</p><p>$scltest = $mw->Scrollbar( );</p><p>$txt =  $mw->Text();</p><p># This method is called when one Listbox is scrolled with the keyboard
  42. # It makes the Scrollbar reflect the change, and scrolls the other lists
  43. sub scroll_Text {
  44.   my ($scltest,   @args) = @_;
  45.   $scltest->set(@args); # tell the Scrollbar what to display $lbs,

  46. }</p><p>
  47. $txt->configure(-yscrollcommand => [ \&scroll_Text, $scltest,
  48.                                         ]);$txt, </p><p>
  49. # Configure the Scrollbar to scroll each Listbox
  50. $scltest->configure(-command => sub {
  51.                                        $txt->yview(@_);
  52.                                      });</p><p># Pack the Scrollbar and Listboxes
  53. $scltest->pack(-side => 'left', -fill => 'y');
  54. $txt->pack(-side => 'left');</p><p>$txt->insert('end', "one", "eight", "nine", "The first argument is the numberof units to scroll by.
  55.                The value for number can be any number, but it's typically either 1 or -1. A value of
  56.                1 means the next unit of data on the bottom or right of the widget becomes visible
  57.                (scrolling one unit of data off the left or top). A value of -1 means that a previous
  58.                unit of data will become visible in the top or right of the widget (one unit will scroll
  59.                off the bottom or right of the widget). For example, every time the user clicks on the
  60.                down arrow in a vertical Scrollbar
  61.                associated with a Listbox, a new line shows up at the bottom of the Listbox. ",
  62.                "This form is used when the user clicks on the slider,
  63.                moves it around, and drops it again. The argument is a fraction, a real number from 0 to 1
  64.                that represents the first part of the data to be shown within the widget. If the user moves
  65.                the slider all the way to the top or left of the Scrollbar, the very first part of the data
  66.                in the widget should be seen on the screen. This means the argument should be 0:");</p><p>
  67. MainLoop;</p><p>
  68. </p><p>
  69. </p>
复制代码


论坛徽章:
0
发表于 2018-02-08 22:43 |显示全部楼层
网上找到了下面有效的例子:
  1. use Tk;
  2. use Tk::Canvas;
  3. use Tk::Label;

  4. my $mw = tkinit(-title, 'Canvas');
  5. #$mw->geometry('250x250');
  6. my $frame = $mw->Frame(-width, 250, -height, 250)->pack(-fill, 'both', -expand,1);
  7. my $canvas = $frame->Scrolled('Canvas', -width, 600, -height, 600,
  8.         -scrollbars, 'osoe'
  9.         )->pack(-fill,'both',-expand,1);

  10. my $btn = $mw->Button(-text, "Add Stuff", -command,\&addStuff)
  11.         ->pack();


  12. MainLoop;

  13. sub addStuff{
  14.         $canvas->createText(500, 500, -text, "Some text @ 500, 500." );
  15.         $canvas->createRectangle(  50,  50, 110, 110, -fill, 'red');
  16.         $canvas->createRectangle( 150, 150, 210, 210, -fill, 'green');
  17.         $canvas->createRectangle( 250, 250, 310, 310, -fill, 'blue');
  18.         $canvas->createText(20, 20, -text, "Some text @ 20, 20." );
  19. #
  20. # Your can use an array for your bounding box
  21. # commented out to show both ways to set the scrollregion
  22. # Just make sure you are 1 less on lower right and bottom
  23. # Or you will have out of bounds array... easy to forget
  24. # if you don't use a frame with your canvas

  25. #$canvas->configure(-scrollregion => [ 0, 0, 599, 599]);
  26.         $canvas->configure(-scrollregion => [$canvas->bbox('all')]);
  27. }

复制代码

论坛徽章:
0
发表于 2018-02-08 22:50 |显示全部楼层
关键是,控件画完后加上下面语句:
$canvas->configure(-scrollregion => [$canvas->bbox('all')]);
或者下面语句指定范围
$canvas->configure(-scrollregion => [ 00, 60, 1200, 800]);

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

数据风云,十年变迁
DTCC 第十届中国数据库技术大会已启航!

2019年5月8日~5月10日,由IT168旗下ITPUB企业社区平台主办的第十届中国数据库技术大会(DTCC2019),将在北京隆重召开。大会将邀请百余位行业专家,就热点技术话题进行分享,是广大数据领域从业人士的又一次年度盛会和交流平台。与SACC2018类似,本届大会将采用“3+2”模式:3天传统技术演讲+2天深度主题培训。大会不仅提供超100场的主题演讲,还会提供连续2天的深度课程培训,深化数据领域的项目落地实践方案。
DTCC2019,一场值得期待的数据技术盛会,殷切地希望您报名参与!

活动入口>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP