为什么我的Perl / Tk Scrolled实际上没有滚动?

时间:2009-06-03 08:44:06

标签: perl tk

以下代码产生了我想要的内容,但左侧的可滚动窗格不会滚动。我正在使用ActivePerl 5.8.9 Build 825:

代码是:

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;
}

我希望有人可以指出我从中遗漏的小事,并结束我的挫败感。

2 个答案:

答案 0 :(得分:3)

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

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

完成此操作后,请了解如何使用strictwarnings pragma。它们在帮助您编写优秀代码方面具有无可估量的价值。正如Sinan指出的那样,不要使用&foo()表示法来调用子程序。这是(非常)旧的语法。这不是必要的,也可能是有害的。最后,我建议对Perl / Tk使用packgrid而不是place。通常更容易获得您想要的行为。只在需要绝对定位时才使用place。您甚至可以混合匹配几何管理器,只要它们管理不同的帧。

答案 1 :(得分:2)

首先观察一下:您的代码并不严格,这使得调试更加困难。您也没有在任何小部件上调用pack。在调用subs时,不应在子名称前使用&符号,除非您知道它的效果并想要它们 - 有关详细信息,请参阅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;