Tk::MListbox 不展开

Tk::MListbox doesn't expand

我想创建一个带有 MListbox 的 Tk 应用程序来显示一些数据。如果信息太多,我希望出现滚动条。

我的问题是 MListbox 没有填满所有可用的 space。右边有个空格space。它看起来不太好。 有可能解决这个问题吗?还是我应该使用另一个小部件? (TableMatrix 似乎很有趣,但我无法下载它)。我选择 MLlistbox 是因为我希望能够隐藏一些列并更改每列的大小。

这是我目前的代码:

my $frameDocuments = $mw->Frame(-background => '#CCCCFF');
    $documentsListbox = $frameDocuments->Scrolled(
        'MListbox',
        -scrollbars => 'osoe',
        -columns => [
                        [-text => 'Name'], [-text => 'Path'], [-text => 'Format'], 
                        [-text => 'Loader Type'], [-text => 'Cache directory']
                    ],
        -resizeable => 1,
        -moveable => 1,
        -sortable => 1,
        -selectmode => 'browse',
    );

$frameDocuments->pack(-anchor => "n",-expand => "1",-fill => "both",-side => "top");
    $documentsListbox->pack(-anchor => "n",-expand => "1",-fill => "both",-side => "top");

当 window 宽度变得大于列宽之和时,Tk::MListbox 似乎没有调整其自己的列的大小。似乎是一个错误,也许你应该报告它?

无论如何,您可以尝试使用 columnPack 函数解决它。根据 documentation:

$ml->columnPack(array)

Repacks all columns in the MListbox widget according to the specification in array. Each element in array is a string on the format index:width. index is a column index, width defines the columns width in pixels (may be omitted). The columns are packed left to right in the order specified by by array. Columns not specified in array will be hidden.

下面是一个示例1,其中我最大化 window 以填充整个屏幕,然后计算列宽:

#! /usr/bin/env perl
use strict;
use warnings;

use Tk;
use Tk::MListbox;

my $mw = MainWindow->new();
my $frameDocuments = $mw->Frame(-background => '#CCCCFF');
my @columns = (
    [-text => 'Name'],
    [-text => 'Path'],
    [-text => 'Format'], 
    [-text => 'Loader Type'],
    [-text => 'Cache directory']
);
my $numCols = scalar @columns;

my $documentsListbox = $frameDocuments->Scrolled(
    'MListbox',
    -scrollbars => 'osoe',
    -columns    => \@columns,
    -resizeable => 1,
    -moveable   => 1,
    -sortable   => 1,
    -selectmode => 'browse',
);

$frameDocuments->pack(
    -anchor => "n",
    -expand => "1",
    -fill   => "both",
    -side => "top"
);
$documentsListbox->pack(
    -anchor => "n",
    -expand => "1",
    -fill => "both",
    -side => "top"
);

my $screenHeight = $mw->screenheight;
my $screenWidth = $mw->screenwidth;
$mw->geometry( sprintf "%dx%d+0+0", $screenWidth, $screenHeight );
my $colWidth = int( $screenWidth / $numCols );
my @ar = map { "$_:$colWidth" } 0 .. ($numCols - 1);
$documentsListbox->columnPack(@ar);

MainLoop;

结果window:

脚注:

  1. 我在代码片段中使用 camelCase 作为变量名,因为您已经在问题中使用了它。请注意 snake_case 在 Perl 中更常见。