2

下面的代码几乎可以产生我想要的东西,但是左侧的可滚动窗格不会滚动。我正在使用 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;
}

我希望有人能指出我错过的小事,结束我的挫败感。

4

2 回答 2

3

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

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

顺便说一句,请了解如何使用strictandwarnings编译指示。它们在帮助您编写好代码方面非常宝贵。正如 Sinan 指出的那样,不要使用该&foo()符号来调用子例程。那是(非常)古老的语法。这不是必需的,并且可能有害。最后,我建议使用packorgrid而不是placePerl/Tk。通常更容易获得您想要的行为。仅place在需要绝对定位时使用。您甚至可以混合搭配几何管理器,只要它们管理不同的帧。

于 2009-06-03T13:33:47.553 回答
2

首先有一些观察:您的代码不是严格安全的,这使得调试更加困难。您也没有在任何小部件上调用 pack。调用 subs 时,不应在子名称前使用 & 符号,除非您知道它的效果并想要它们——有关详细信息,请参阅perldoc perlsub

三个面板的宽度加起来有800多。你想要的布局不是那么清晰。

现在,这里有一些代码(从 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;
于 2009-06-03T11:51:39.473 回答