2009-06-03 101 views
2

以下代码几乎可以生成我想要的内容,但左侧的可滚动窗格不会滚动。我使用的ActivePerl 5.8.9建立825:为什么我的Perl/Tk滚动实际上不滚动?

的代码是:

use Tk; 
use Tk::Pane; 
use Tk::LabFrame; 

# create the application window 
my $MW = MainWindow->new (-background => "GREY"); 

# set the x/y size for the window 
$MW->geometry("800x600"); 

# add a window title 
$MW->title("Monitor Boxes"); 

# Disallow window resizing 
$MW->resizable(0,0); 

# create a labelled frame on the window to house the list of buttons to be pressed 
$boxListFrame = &create_framed_section("List Of Boxes", 
        "acrosstop", 
        5, 
        5, 
        170, 
        565, 
        "BLUE", 
        "GREY"); 

# create a labelled frame on the window to house the information to be displayed 
# when a particular button is pressed 
$statusOfBoxFrame = &create_framed_section("Status", 
         "acrosstop", 
         185, 
         5, 
         600, 
         565, 
         "BLUE", 
         "GREY"); 

# create a scrollable pane in the left hand pane so that if more buttons than 
# is able to be displayed are put onto the application the scroll bar will allow 
# the ones not displayed to be access 
my $pane = $MW->Scrolled( 'Pane', 
       -scrollbars => 'e', 
       -width => 140, 
       -height => 555, 
       -background => "GREY")->place(-x=>15,-y=>25); 

# setup the array of buttons 
@boxes = ("BUTTON1", "BUTTON2", "BUTTON3", "BUTTON4", "BUTTON5", "BUTTON6", "BUTTON7", "BUTTON8", "BUTTON9", "BUTTON10", "BUTTON11", "BUTTON12", "BUTTON13", "BUTTON14", "BUTTON15", "BUTTON16"); 

# put the buttons onto the scrollable pane in the frame on the window (LOL) 
DisplayCheckButtons($pane, @boxes); 

# wait until the user exits the app 
MainLoop; 

# exit the app 
exit 0; 

sub DisplayCheckButtons 
{ 
    my ($parent, @names) = @_; 
    $Frame->destroy if $Frame; 
    $Frame = $parent->Frame( -width => 160, 
        -height => 555, 
        -background => "GREY")->place(-x=>15,-y=>15); 

    $xpos = 5; 
    $ypos = 5; 

    foreach $box (@names) 
    { 
     $buttons->{$box} = $Frame->Button(-text => $box)->place(-x=>$xpos,-y=>$ypos); 

     $ypos = $ypos + 40; 

    } 
} 

sub create_framed_section 
{ 
    # get the parameters 
    my($label, $labelside, $posX, $posY, $width, $height, $fontColour, $backgroundColour) = @_; 

    # create the item in the desired position with supplied information 
    $frame = $MW->LabFrame(-label  => $label, 
       -labelside => $labelside, 
       -width  => $width, 
       -height  => $height, 
       -foreground => $fontColour, 
       -background => $backgroundColour, 
       )->place(-x=>$posX,-y=>$posY); 

    return $frame; 
} 

我希望有人能指出我从这里错过了次要的事情,结束我的无奈。

+0

格伦,当你使用严格的,你需要声明你的变量,然后再使用它们。我的$ foo ='bar';或者在你的例子中,我的$按钮; $ button - > {$ box} = $ parent-> Button(-text => $ box) - > pack(-pady => 10); – daotoad 2009-06-03 15:08:58

回答

2

首先有几点看法:您的代码并不严格安全,这使得调试变得更加困难。您也不会在任何小部件上打包。除非您知道其效果并希望它们,否则不应在调用子类时在子名称前使用&符号 - 有关详细信息,请参阅perldoc perlsub

三个面板的宽度加起来超过800.你想要的布局不那么清晰。

现在,这里是一些代码(stolen from PerlMongers)与一堆按钮创建一个滚动窗格:

#!/usr/bin/perl 

use strict; 
use warnings; 

use Tk; 
use Tk::Pane; 

my $top = MainWindow->new; 
$top->Label(-text => "Enter the scroll frame")->pack; 

my $frame = $top->Scrolled(
    'Frame', 
    -scrollbars => "e", 
)->pack; 

$frame->Button(-text => "BUTTON $_")->pack for (1 .. 16); 

MainLoop; 
+0

他使用“地点”几何管理器而不是“包”(或“网格”)。 – 2009-06-03 13:15:13

+0

我不使用Tk,所以我没有意识到这一点。谢谢你指出。 – 2009-06-03 14:14:13

3

你有两个问题。首先,您应该直接使用Pane小部件,而不是在其中创建另一个Frame。另一个问题似乎是在Pane小部件中使用place几何体管理器。请尝试使用pack

$buttons->{$box} = $parent->Button(-text => $box)->pack(-pady => 10); 

有了这样的方式,请了解如何使用strictwarnings编译指示。它们对于帮助您编写出色的代码非常宝贵。正如Sinan指出的那样,不要使用&foo()表示法来调用子程序。这是(非常)古老的语法。这不是必要的,并且可能有害。最后,我建议对Perl/Tk使用packgrid而不是place。获得您想要的行为通常更容易。只有在需要绝对定位时才使用place。只要管理不同的框架,您甚至可以混合搭配几何管理器。