2017-07-31 65 views
1

我想用MListbox創建一個Tk應用程序來顯示一些數據。如果信息太多,我希望顯示一個滾動條。Tk :: MListbox不擴展

我的問題是MListbox沒有填充所有可用空間。右側有一個空白區域。它看起來不太好。 有沒有可能解決這個問題?或者我應該使用另一個小部件? (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"); 
+0

作爲一種變通方法,你可以嘗試使用'$ documentsListbox-> columnPack(@ar)',其中'@ ar'包含列的大小。例如,由於您有5列,您可以嘗試製作每個屏幕寬度的1/5。 –

回答

0

這似乎是Tk::MListbox當窗口寬度比列寬的總和不調整自己的列。看起來像一個錯誤,也許你應該報告它?

無論如何,你可以嘗試使用columnPack函數來解決它。根據documentation

$ ML-> columnPack(陣列)

重新包裝的所有列在MListbox插件 根據在陣列的規格。數組中的每個元素是格式爲index:width的 字符串。索引是列索引,寬度爲 定義了以像素爲單位的列寬(可以省略)。這些列是 按數組指定的順序從左到右排列。在數組中指定的列不會被 隱藏。

下面是一個例子其中I最大化窗口以充滿整個屏幕,然後計算列寬:

#! /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; 

結果窗口

enter image description here

腳註

  1. 我以前駝峯變量名中的代碼片段,因爲你已經在你的問題中使用它。請注意,snake_case在Perl中更常見。

+0

非常感謝您的回答。 – Shadow

+0

這太糟糕了,它不會自動擴展。然後我會使用你的解決方案。如果我想能夠擴展窗口,我將不得不綁定到窗口大小修改。或者,也許我會簡單地禁止窗口修改。再次感謝。 – Shadow

+0

是的,它是一個可憐的人。也許你可以檢查[源](https:// metacpan。org/source/RCS/Tk-MListbox-1.11/MListbox.pm),並向作者提出改進建議(如果他仍然在那裏,因爲看起來像15年前他最後更新了任何東西)。但綁定到配置如您所說,也應該讓您手動進行列表框列的更新 –